Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a91fd6a

Browse files
authored
Create 09. PromiseAll.md
1 parent 07b9040 commit a91fd6a

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

‎Machine Coding/09. PromiseAll.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
```js
2+
/**
3+
* Implements Promise.all functionality.
4+
*
5+
* @param {Iterable} iterable - An array (or any iterable) of promises or values.
6+
* @returns {Promise} - A promise that resolves when all input promises resolve,
7+
* or rejects when any input promise rejects.
8+
*/
9+
export default function promiseAll(iterable) {
10+
if (!iterable || typeof iterable[Symbol.iterator] !== 'function') {
11+
// Ensure the input is an iterable
12+
throw new TypeError('Expected an iterable');
13+
}
14+
15+
return new Promise((resolve, reject) => {
16+
const results = []; // Array to store resolved values
17+
let completed = 0; // Counter for how many promises have been resolved
18+
const promises = Array.from(iterable); // Convert iterable to an array
19+
20+
if (promises.length === 0) {
21+
// Handle the case where the iterable is empty
22+
resolve([]);
23+
}
24+
25+
// Iterate through all promises
26+
promises.forEach((promise, index) => {
27+
// Handle a promise or value; use Promise.resolve to wrap non-promise values
28+
Promise.resolve(promise)
29+
.then((value) => {
30+
// Store the resolved value at the corresponding index
31+
results[index] = value;
32+
completed += 1;
33+
34+
// If all promises are resolved, resolve the result
35+
if (completed === promises.length) {
36+
resolve(results);
37+
}
38+
})
39+
.catch((error) => {
40+
// If any promise rejects, reject the entire promise
41+
reject(error);
42+
});
43+
});
44+
});
45+
}
46+
```
47+
### Explanation
48+
#### Validate Input:
49+
50+
* Check if the input iterable is an object and has the iterator protocol `(Symbol.iterator)`. If not, throw a TypeError.
51+
#### Handle Empty Input:
52+
53+
* If the iterable is empty, resolve immediately with an empty array ([]).
54+
#### Iterate Over Promises:
55+
56+
* Convert the iterable into an array using Array.from(iterable) for uniform handling.
57+
* Iterate over the array of promises and wrap all values using Promise.resolve. Non-promise values are converted to promises that resolve immediately.
58+
#### Track Resolutions:
59+
60+
* Maintain an array (results) to store resolved values at the correct indices.
61+
* Use a counter (completed) to track how many promises have successfully resolved.
62+
#### Resolve When All Promises Succeed:
63+
64+
* Once the completed counter equals the total number of promises, resolve the final promise with the results array.
65+
#### Reject on Any Failure:
66+
67+
* If any promise rejects, immediately reject the resulting promise with the error.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /