Created
February 1, 2025 16:02
-
-
Save mityaua/60ee56566e5bffd3a07627f29c210b12 to your computer and use it in GitHub Desktop.
Implementing Promise.all and Promise.allSettled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const promiseAll = (promises) => { | |
return new Promise((resolve, reject) => { | |
let results = []; | |
let completed = 0; | |
promises.forEach((promise, index) => { | |
promise | |
.then((value) => { | |
completed += 1; | |
results[index] = { status: "fulfilled", value }; | |
}) | |
.catch(error => { | |
reject(error) | |
}) | |
.finally(() => { | |
if (completed === promises.length) { | |
resolve(results); | |
} | |
}); | |
}); | |
}); | |
}; | |
const promiseAllSettled = (promises) => { | |
return new Promise((resolve) => { | |
let results = []; | |
let completed = 0; | |
promises.forEach((promise, index) => { | |
promise | |
.then((value) => { | |
results[index] = { status: "fulfilled", value }; | |
}) | |
.catch((reason) => { | |
results[index] = { status: "rejected", reason }; | |
}) | |
.finally(() => { | |
completed += 1; | |
if (completed === promises.length) { | |
resolve(results); | |
} | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment