Skip to content

Commit

Permalink
lib: reduce overhead of SafePromiseAllSettledReturnVoid calls
Browse files Browse the repository at this point in the history
It was simply calling `SafePromiseAllSettled`, which itself calls
`arrayToSafePromiseIterable` which wraps all promises into new
`SafePromise` object and wraps it into a `SafeArrayIterator`. Since we
don't care about the return value, we can take some shortcuts.

PR-URL: #51243
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored and RafaelGSS committed Jan 2, 2024
1 parent 96d64ed commit ed7305e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,17 @@ primordials.SafePromiseAllSettled = (promises, mapFn) =>
* @param {(v: T|PromiseLike<T>, k: number) => U|PromiseLike<U>} [mapFn]
* @returns {Promise<void>}
*/
primordials.SafePromiseAllSettledReturnVoid = async (promises, mapFn) => {
await primordials.SafePromiseAllSettled(promises, mapFn);
};
primordials.SafePromiseAllSettledReturnVoid = (promises, mapFn) => new Promise((resolve) => {
let pendingPromises = promises.length;
if (pendingPromises === 0) resolve();
const onSettle = () => {
if (--pendingPromises === 0) resolve();
};
for (let i = 0; i < promises.length; i++) {
const promise = mapFn != null ? mapFn(promises[i], i) : promises[i];
PromisePrototypeThen(PromiseResolve(promise), onSettle, onSettle);
}
});

/**
* @template T,U
Expand Down

0 comments on commit ed7305e

Please sign in to comment.