diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2022-10-29 18:25:23 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-29 18:25:23 +0900 |
commit | 59ac110edd1f376bed7fa6bbdbe2ee09c266bf74 (patch) | |
tree | da654d1ecb6141b620141d634d99ca34c6d568db /core/00_primordials.js | |
parent | edaceecec771cf0395639175b5a21d20530f6080 (diff) |
fix(core): fix APIs not to be affected by `Promise.prototype.then` modification (#16326)
Diffstat (limited to 'core/00_primordials.js')
-rw-r--r-- | core/00_primordials.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/core/00_primordials.js b/core/00_primordials.js index 843eb8b29..d48dfde79 100644 --- a/core/00_primordials.js +++ b/core/00_primordials.js @@ -275,12 +275,15 @@ const { ArrayPrototypeForEach, + ArrayPrototypeMap, FunctionPrototypeCall, Map, ObjectDefineProperty, ObjectFreeze, + ObjectPrototypeIsPrototypeOf, ObjectSetPrototypeOf, Promise, + PromisePrototype, PromisePrototypeThen, Set, SymbolIterator, @@ -437,6 +440,29 @@ PromisePrototypeThen(thisPromise, undefined, onRejected); /** + * Creates a Promise that is resolved with an array of results when all of the + * provided Promises resolve, or rejected when any Promise is rejected. + * @param {unknown[]} values An array of Promises. + * @returns A new Promise. + */ + primordials.SafePromiseAll = (values) => + // Wrapping on a new Promise is necessary to not expose the SafePromise + // prototype to user-land. + new Promise((a, b) => + SafePromise.all( + ArrayPrototypeMap( + values, + (p) => { + if (ObjectPrototypeIsPrototypeOf(PromisePrototype, p)) { + return new SafePromise((c, d) => PromisePrototypeThen(p, c, d)); + } + return p; + }, + ), + ).then(a, b) + ); + + /** * Attaches a callback that is invoked when the Promise is settled (fulfilled or * rejected). The resolved value cannot be modified from the callback. * Prefer using async functions when possible. |