From 59ac110edd1f376bed7fa6bbdbe2ee09c266bf74 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Sat, 29 Oct 2022 18:25:23 +0900 Subject: fix(core): fix APIs not to be affected by `Promise.prototype.then` modification (#16326) --- core/00_primordials.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'core/00_primordials.js') 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, @@ -436,6 +439,29 @@ primordials.PromisePrototypeCatch = (thisPromise, onRejected) => 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. -- cgit v1.2.3