Skip to content

Commit c789c0f

Browse files
aduh95danielleadams
authored andcommitted
doc: improve documentation for safe Promise statics alternatives
PR-URL: #43759 Refs: #43728 Reviewed-By: Feng Yu <F3n67u@outlook.com>
1 parent 9435fbf commit c789c0f

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

doc/contributing/primordials.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,33 @@ Object.defineProperty(Object.prototype, Symbol.isConcatSpreadable, {
363363
// 1. Lookup @@iterator property on `array` (user-mutable if user-provided).
364364
// 2. Lookup @@iterator property on %Array.prototype% (user-mutable).
365365
// 3. Lookup `next` property on %ArrayIteratorPrototype% (user-mutable).
366-
PromiseAll(array); // unsafe
366+
PromiseAll([]); // unsafe
367+
368+
PromiseAll(new SafeArrayIterator([])); // safe
369+
370+
const array = [promise];
371+
const set = new SafeSet().add(promise);
372+
// When running one of these functions on a non-empty iterable, it will also:
373+
// 4. Lookup `then` property on `promise` (user-mutable if user-provided).
374+
// 5. Lookup `then` property on `%Promise.prototype%` (user-mutable).
375+
PromiseAll(new SafeArrayIterator(array)); // unsafe
376+
377+
PromiseAll(set); // unsafe
367378

368-
PromiseAll(new SafeArrayIterator(array));
369379
SafePromiseAll(array); // safe
380+
381+
// Some key differences between `SafePromise[...]` and `Promise[...]` methods:
382+
383+
// 1. SafePromiseAll, SafePromiseAllSettled, SafePromiseAny, and SafePromiseRace
384+
// support passing a mapperFunction as second argument.
385+
SafePromiseAll(ArrayPrototypeMap(array, someFunction));
386+
SafePromiseAll(array, someFunction); // Same as the above, but more efficient.
387+
388+
// 2. SafePromiseAll, SafePromiseAllSettled, SafePromiseAny, and SafePromiseRace
389+
// only support arrays, not iterables. Use ArrayFrom to convert an iterable
390+
// to an array.
391+
SafePromiseAll(set); // ignores set content.
392+
SafePromiseAll(ArrayFrom(set)); // safe
370393
```
371394

372395
</details>
@@ -459,6 +482,7 @@ original methods:
459482
* It expects an array (or array-like object) instead of an iterable.
460483
* It wraps each promise in `SafePromise` objects and wraps the result in a new
461484
`Promise` instance – which may come with a performance penalty.
485+
* It accepts a `mapperFunction` as second argument.
462486
* Because it doesn't look up `then` property, it may not be the right tool to
463487
handle user-provided promises (which may be instances of a subclass of
464488
`Promise`).
@@ -493,6 +517,15 @@ PromisePrototypeThen(
493517
process.on('exit', () => console.log(thenBlockExecuted)); // true
494518
```
495519

520+
A common pattern is to map on the array of `Promise`s to apply some
521+
transformations, in that case it can be more efficient to pass a second argument
522+
rather than invoking `%Array.prototype.map%`.
523+
524+
```js
525+
SafePromiseAll(ArrayPrototypeMap(array, someFunction));
526+
SafePromiseAll(array, someFunction); // Same as the above, but more efficient.
527+
```
528+
496529
</details>
497530

498531
### (Async) Generator functions

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy