Skip to content

Commit fffed88

Browse files
authored
Create Promise.all.js
1 parent debee84 commit fffed88

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Promise.all.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const sleep = (duration) =>
2+
new Promise((resolve) => setTimeout(() => resolve(duration), duration));
3+
4+
async function all(list) {
5+
return new Promise((res, rej) => {
6+
let count = 0;
7+
const result = [];
8+
9+
list.forEach((item) => {
10+
Promise.resolve(item)
11+
.then((data) => {
12+
result.push(data);
13+
count++;
14+
15+
if (count === list.length) {
16+
res(result);
17+
}
18+
})
19+
.catch(rej);
20+
});
21+
});
22+
}
23+
24+
all([sleep(1000), sleep(2000), sleep(3000)]).then((res) => {
25+
console.log(res);
26+
});

0 commit comments

Comments
 (0)