| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -363,10 +363,33 @@ Object.defineProperty(Object.prototype, Symbol.isConcatSpreadable, { | |||
| 363 | 363 | // 1. Lookup @@iterator property on `array` (user-mutable if user-provided). | |
| 364 | 364 | // 2. Lookup @@iterator property on %Array.prototype% (user-mutable). | |
| 365 | 365 | // 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 | ||
| 367 | 378 | ||
| 368 | - PromiseAll(new SafeArrayIterator(array)); | ||
| 369 | 379 | 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 | ||
| 370 | 393 | ``` | |
| 371 | 394 | ||
| 372 | 395 | </details> | |
@@ -459,6 +482,7 @@ original methods: | |||
| 459 | 482 | * It expects an array (or array-like object) instead of an iterable. | |
| 460 | 483 | * It wraps each promise in `SafePromise` objects and wraps the result in a new | |
| 461 | 484 | `Promise` instance – which may come with a performance penalty. | |
| 485 | + * It accepts a `mapperFunction` as second argument. | ||
| 462 | 486 | * Because it doesn't look up `then` property, it may not be the right tool to | |
| 463 | 487 | handle user-provided promises (which may be instances of a subclass of | |
| 464 | 488 | `Promise`). | |
@@ -493,6 +517,15 @@ PromisePrototypeThen( | |||
| 493 | 517 | process.on('exit', () => console.log(thenBlockExecuted)); // true | |
| 494 | 518 | ``` | |
| 495 | 519 | ||
| 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 | + | ||
| 496 | 529 | </details> | |
| 497 | 530 | ||
| 498 | 531 | ### (Async) Generator functions | |
| Back | FazBrowse Home | New Git URL |
0 commit comments