summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/00_primordials.js26
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.