Skip to content

Instantly share code, notes, and snippets.

@mityaua
Created February 1, 2025 16:02
Show Gist options
  • Save mityaua/60ee56566e5bffd3a07627f29c210b12 to your computer and use it in GitHub Desktop.
Save mityaua/60ee56566e5bffd3a07627f29c210b12 to your computer and use it in GitHub Desktop.
Implementing Promise.all and Promise.allSettled
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