does anyone know of a way I can determine how long each promise took to execute in Promise.all([...])
?
Thanks in advance!
does anyone know of a way I can determine how long each promise took to execute in Promise.all([...])
?
Thanks in advance!
You could make your own Promise.all function.
Below is an example, that returns timings for each promise in an array it returns, the array also contains the results of the promises too, but of course in this demo my wait
function has nothing to return.
function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function myPromiseAll(promises) {
const starttime = new Date().getTime();
const timings = [];
promises.forEach((prom, ix) => {
prom.then(() => {
timings[ix] = new Date().getTime() - starttime;
});
});
const result = await Promise.all(promises);
return {result, timings};
}
async function run() {
console.log("Starting stuff.");
const ret = await myPromiseAll([
wait(1000), wait(2000)]);
console.log(ret.timings);
// console.log(ret.result); result of promises
}
run();