Skip to content

Commit a86ef1b

Browse files
aduh95RafaelGSS
authored andcommitted
lib: use safe Promise alternatives when available
PR-URL: #43476 Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 2be8aca commit a86ef1b

File tree

6 files changed

+27
-37
lines changed

6 files changed

+27
-37
lines changed

lib/internal/debugger/inspect_repl.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const {
2323
ObjectKeys,
2424
ObjectValues,
2525
Promise,
26-
PromisePrototypeCatch,
2726
PromisePrototypeThen,
2827
PromiseResolve,
2928
ReflectGetOwnPropertyDescriptor,
@@ -653,8 +652,8 @@ function createRepl(inspector) {
653652
}
654653

655654
const inspectValue = (expr) =>
656-
PromisePrototypeCatch(evalInCurrentContext(expr),
657-
(error) => `<${error.message}>`);
655+
PromisePrototypeThen(evalInCurrentContext(expr), undefined,
656+
(error) => `<${error.message}>`);
658657
const lastIndex = watchedExpressions.length - 1;
659658

660659
const values = await SafePromiseAll(watchedExpressions, inspectValue);

lib/internal/fs/cp/cp.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ const {
66
ArrayPrototypeEvery,
77
ArrayPrototypeFilter,
88
Boolean,
9-
PromiseAll,
10-
PromisePrototypeCatch,
119
PromisePrototypeThen,
1210
PromiseReject,
13-
SafeArrayIterator,
11+
SafePromiseAll,
1412
StringPrototypeSplit,
1513
} = primordials;
1614
const {
@@ -128,13 +126,13 @@ function getStats(src, dest, opts) {
128126
const statFunc = opts.dereference ?
129127
(file) => stat(file, { bigint: true }) :
130128
(file) => lstat(file, { bigint: true });
131-
return PromiseAll(new SafeArrayIterator([
129+
return SafePromiseAll([
132130
statFunc(src),
133-
PromisePrototypeCatch(statFunc(dest), (err) => {
131+
PromisePrototypeThen(statFunc(dest), undefined, (err) => {
134132
if (err.code === 'ENOENT') return null;
135133
throw err;
136134
}),
137-
]));
135+
]);
138136
}
139137

140138
async function checkParentDir(destStat, src, dest, opts) {

lib/internal/modules/esm/loader.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ const {
1313
ObjectCreate,
1414
ObjectDefineProperty,
1515
ObjectSetPrototypeOf,
16-
PromiseAll,
1716
RegExpPrototypeExec,
18-
SafeArrayIterator,
17+
SafePromiseAll,
1918
SafeWeakMap,
2019
StringPrototypeSlice,
2120
StringPrototypeToUpperCase,
@@ -525,7 +524,7 @@ class ESMLoader {
525524
.then(({ module }) => module.getNamespace());
526525
}
527526

528-
const namespaces = await PromiseAll(new SafeArrayIterator(jobs));
527+
const namespaces = await SafePromiseAll(jobs);
529528

530529
if (!wasArr) { return namespaces[0]; } // We can skip the pairing below
531530

lib/internal/webstreams/adapters.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
22

33
const {
4-
ArrayPrototypeMap,
5-
PromiseAll,
64
PromisePrototypeThen,
7-
PromisePrototypeFinally,
85
PromiseResolve,
6+
SafePromiseAll,
7+
SafePromisePrototypeFinally,
98
Uint8Array,
109
} = primordials;
1110

@@ -165,7 +164,7 @@ function newWritableStreamFromStreamWritable(streamWritable) {
165164
async write(chunk) {
166165
if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) {
167166
backpressurePromise = createDeferredPromise();
168-
return PromisePrototypeFinally(
167+
return SafePromisePrototypeFinally(
169168
backpressurePromise.promise, () => {
170169
backpressurePromise = undefined;
171170
});
@@ -246,10 +245,9 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
246245
writer.ready,
247246
() => {
248247
return PromisePrototypeThen(
249-
PromiseAll(
250-
ArrayPrototypeMap(
251-
chunks,
252-
(data) => writer.write(data.chunk))),
248+
SafePromiseAll(
249+
chunks,
250+
(data) => writer.write(data.chunk)),
253251
done,
254252
done);
255253
},
@@ -668,10 +666,9 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
668666
writer.ready,
669667
() => {
670668
return PromisePrototypeThen(
671-
PromiseAll(
672-
ArrayPrototypeMap(
673-
chunks,
674-
(data) => writer.write(data.chunk))),
669+
SafePromiseAll(
670+
chunks,
671+
(data) => writer.write(data.chunk)),
675672
done,
676673
done);
677674
},
@@ -767,7 +764,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
767764

768765
if (!writableClosed || !readableClosed) {
769766
PromisePrototypeThen(
770-
PromiseAll([
767+
SafePromiseAll([
771768
closeWriter(),
772769
closeReader(),
773770
]),

lib/internal/webstreams/readablestream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const {
1919
PromisePrototypeThen,
2020
PromiseResolve,
2121
PromiseReject,
22-
PromiseAll,
2322
ReflectConstruct,
23+
SafePromiseAll,
2424
Symbol,
2525
SymbolAsyncIterator,
2626
SymbolToStringTag,
@@ -1334,7 +1334,7 @@ function readableStreamPipeTo(
13341334
}
13351335

13361336
shutdownWithAnAction(
1337-
async () => PromiseAll(actions.map((action) => action())),
1337+
() => SafePromiseAll(actions, (action) => action()),
13381338
true,
13391339
error);
13401340
}

lib/internal/webstreams/transformstream.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
FunctionPrototypeBind,
55
FunctionPrototypeCall,
66
ObjectDefineProperties,
7-
PromisePrototypeCatch,
87
PromisePrototypeThen,
98
PromiseResolve,
109
ReflectConstruct,
@@ -496,19 +495,17 @@ function transformStreamDefaultControllerError(controller, error) {
496495
transformStreamError(controller[kState].stream, error);
497496
}
498497

499-
function transformStreamDefaultControllerPerformTransform(controller, chunk) {
500-
const transformPromise =
501-
ensureIsPromise(
498+
async function transformStreamDefaultControllerPerformTransform(controller, chunk) {
499+
try {
500+
return await ensureIsPromise(
502501
controller[kState].transformAlgorithm,
503502
controller,
504503
chunk,
505504
controller);
506-
return PromisePrototypeCatch(
507-
transformPromise,
508-
(error) => {
509-
transformStreamError(controller[kState].stream, error);
510-
throw error;
511-
});
505+
} catch (error) {
506+
transformStreamError(controller[kState].stream, error);
507+
throw error;
508+
}
512509
}
513510

514511
function transformStreamDefaultControllerTerminate(controller) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy