A Promise.race
implementation that works with JavaScript map.
Promise.race
is useful when you need either one of the Promise to complete before proceeding. But if you need to process the result, it isn't easy to distinguish between two promises.
Before using this package, you will need to write code like this.
const p1 = fn1();
const p2 = fn2();
// Given both Promise do not return falsy values
const { r1, r2 } = await Promise.race([
() => p1.then(res => ({ r1: res })),
() => p2.then(res => ({ r2: res }))
]);
if (r1) {
// Process result from fn1()
} else {
// Process result from fn2()
}
After using this package, you can write shorter and more readable code like this.
const { r1, r2 } = await Promise.race({
r1: fn1(),
r2: fn2()
});
if (r1) {
// Process result from fn1()
} else {
// Process result from fn2()
}
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.