How can I implement Promise.race() method with async and await?
async function promiseRace(promises) {
const results = [];
for (const p of promises) {
await p;
results.push(p);
}
return results[0];
}
I've tried to implement it like above but this doesn't work.
You can't. Just like you cannot implement the Promise
constructor using async
/await
. Remember that await
is only syntactic sugar for then
calls - and you cannot implement the basic promise combinators using only that.