Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! lib: implement safe alternatives to Promise static methods
  • Loading branch information
aduh95 committed Jul 8, 2022
commit 418864eb20bee4b71f641e809a43511dde8b3af2
4 changes: 1 addition & 3 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSome,
FunctionPrototype,
Expand Down Expand Up @@ -111,8 +110,7 @@ class ModuleJob {
}
jobsInGraph.add(moduleJob);
const dependencyJobs = await moduleJob.linked;
return SafePromiseAll(
ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph));
return SafePromiseAll(dependencyJobs, addJobsToDependencyGraph);
};
await addJobsToDependencyGraph(this);

Expand Down
28 changes: 16 additions & 12 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,57 +435,61 @@ primordials.AsyncIteratorPrototype =
primordials.ReflectGetPrototypeOf(
async function* () {}).prototype);

const arrayToSafePromiseIterable = (promises) =>
const arrayToSafePromiseIterable = (promises, mapFn) =>
new primordials.SafeArrayIterator(
ArrayPrototypeMap(
promises,
(promise) =>
new SafePromise((a, b) => PromisePrototypeThen(promise, a, b))
(promise, i) =>
new SafePromise((a, b) => PromisePrototypeThen(mapFn == null ? promise : mapFn(promise, i), a, b))
)
);

/**
* @param {Promise<any>[]} promises
* @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn]
* @returns {Promise<any[]>}
*/
primordials.SafePromiseAll = (promises) =>
primordials.SafePromiseAll = (promises, mapFn) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.all(arrayToSafePromiseIterable(promises)).then(a, b)
SafePromise.all(arrayToSafePromiseIterable(promises, mapFn)).then(a, b)
);

/**
* @param {Promise<any>[]} promises
* @returns {Promise<object>}
* @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn]
* @returns {Promise<PromiseSettledResult<any>[]>}
*/
primordials.SafePromiseAllSettled = (promises) =>
primordials.SafePromiseAllSettled = (promises, mapFn) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.allSettled(arrayToSafePromiseIterable(promises)).then(a, b)
SafePromise.allSettled(arrayToSafePromiseIterable(promises, mapFn)).then(a, b)
);

/**
* @param {Promise<any>[]} promises
* @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn]
* @returns {Promise<any>}
*/
primordials.SafePromiseAny = (promises) =>
primordials.SafePromiseAny = (promises, mapFn) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.any(arrayToSafePromiseIterable(promises)).then(a, b)
SafePromise.any(arrayToSafePromiseIterable(promises, mapFn)).then(a, b)
);

/**
* @param {Promise<any>[]} promises
* @param {(v: Promise<any>, k: number) => Promise<any>} [mapFn]
* @returns {Promise<any>}
*/
primordials.SafePromiseRace = (promises) =>
primordials.SafePromiseRace = (promises, mapFn) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.race(arrayToSafePromiseIterable(promises)).then(a, b)
SafePromise.race(arrayToSafePromiseIterable(promises, mapFn)).then(a, b)
);


Expand Down