lib: use safe Promise alternatives when available

PR-URL: https://github.com/nodejs/node/pull/43476
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
Antoine du Hamel authored and Juan José Arboleda committed 2022-10-11 14:45:18 -05:00
1 parent bad8ecc27f
commit 59be5f89ca
6 files changed
+27 -37

No files matched your search

+2 -3
View File
@@ -23,7 +23,6 @@ const {
ObjectKeys,
ObjectValues,
Promise,
PromisePrototypeCatch,
PromisePrototypeThen,
PromiseResolve,
ReflectGetOwnPropertyDescriptor,
@@ -653,8 +652,8 @@ function createRepl(inspector) {
}
const inspectValue = (expr) =>
PromisePrototypeCatch(evalInCurrentContext(expr),
(error) => `<${error.message}>`);
PromisePrototypeThen(evalInCurrentContext(expr), undefined,
(error) => `<${error.message}>`);
const lastIndex = watchedExpressions.length - 1;
const values = await SafePromiseAll(watchedExpressions, inspectValue);
+4 -6
View File
@@ -6,11 +6,9 @@ const {
ArrayPrototypeEvery,
ArrayPrototypeFilter,
Boolean,
PromiseAll,
PromisePrototypeCatch,
PromisePrototypeThen,
PromiseReject,
SafeArrayIterator,
SafePromiseAll,
StringPrototypeSplit,
} = primordials;
const {
@@ -128,13 +126,13 @@ function getStats(src, dest, opts) {
const statFunc = opts.dereference ?
(file) => stat(file, { bigint: true }) :
(file) => lstat(file, { bigint: true });
return PromiseAll(new SafeArrayIterator([
return SafePromiseAll([
statFunc(src),
PromisePrototypeCatch(statFunc(dest), (err) => {
PromisePrototypeThen(statFunc(dest), undefined, (err) => {
if (err.code === 'ENOENT') return null;
throw err;
}),
]));
]);
}
async function checkParentDir(destStat, src, dest, opts) {
+2 -3
View File
@@ -13,9 +13,8 @@ const {
ObjectCreate,
ObjectDefineProperty,
ObjectSetPrototypeOf,
PromiseAll,
RegExpPrototypeExec,
SafeArrayIterator,
SafePromiseAll,
SafeWeakMap,
StringPrototypeSlice,
StringPrototypeToUpperCase,
@@ -525,7 +524,7 @@ class ESMLoader {
.then(({ module }) => module.getNamespace());
}
const namespaces = await PromiseAll(new SafeArrayIterator(jobs));
const namespaces = await SafePromiseAll(jobs);
if (!wasArr) { return namespaces[0]; } // We can skip the pairing below
+10 -13
View File
@@ -1,11 +1,10 @@
'use strict';
const {
ArrayPrototypeMap,
PromiseAll,
PromisePrototypeThen,
PromisePrototypeFinally,
PromiseResolve,
SafePromiseAll,
SafePromisePrototypeFinally,
Uint8Array,
} = primordials;
@@ -165,7 +164,7 @@ function newWritableStreamFromStreamWritable(streamWritable) {
async write(chunk) {
if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) {
backpressurePromise = createDeferredPromise();
return PromisePrototypeFinally(
return SafePromisePrototypeFinally(
backpressurePromise.promise, () => {
backpressurePromise = undefined;
});
@@ -246,10 +245,9 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
writer.ready,
() => {
return PromisePrototypeThen(
PromiseAll(
ArrayPrototypeMap(
chunks,
(data) => writer.write(data.chunk))),
SafePromiseAll(
chunks,
(data) => writer.write(data.chunk)),
done,
done);
},
@@ -652,10 +650,9 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
writer.ready,
() => {
return PromisePrototypeThen(
PromiseAll(
ArrayPrototypeMap(
chunks,
(data) => writer.write(data.chunk))),
SafePromiseAll(
chunks,
(data) => writer.write(data.chunk)),
done,
done);
},
@@ -751,7 +748,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
if (!writableClosed || !readableClosed) {
PromisePrototypeThen(
PromiseAll([
SafePromiseAll([
closeWriter(),
closeReader(),
]),
+2 -2
View File
@@ -19,8 +19,8 @@ const {
PromisePrototypeThen,
PromiseResolve,
PromiseReject,
PromiseAll,
ReflectConstruct,
SafePromiseAll,
Symbol,
SymbolAsyncIterator,
SymbolToStringTag,
@@ -1319,7 +1319,7 @@ function readableStreamPipeTo(
}
shutdownWithAnAction(
async () => PromiseAll(actions.map((action) => action())),
() => SafePromiseAll(actions, (action) => action()),
true,
error);
}
+7 -10
View File
@@ -4,7 +4,6 @@ const {
FunctionPrototypeBind,
FunctionPrototypeCall,
ObjectDefineProperties,
PromisePrototypeCatch,
PromisePrototypeThen,
PromiseResolve,
ReflectConstruct,
@@ -496,19 +495,17 @@ function transformStreamDefaultControllerError(controller, error) {
transformStreamError(controller[kState].stream, error);
}
function transformStreamDefaultControllerPerformTransform(controller, chunk) {
const transformPromise =
ensureIsPromise(
async function transformStreamDefaultControllerPerformTransform(controller, chunk) {
try {
return await ensureIsPromise(
controller[kState].transformAlgorithm,
controller,
chunk,
controller);
return PromisePrototypeCatch(
transformPromise,
(error) => {
transformStreamError(controller[kState].stream, error);
throw error;
});
} catch (error) {
transformStreamError(controller[kState].stream, error);
throw error;
}
}
function transformStreamDefaultControllerTerminate(controller) {