promise-race-map

1.0.0 • Public • Published

promise-race-map

npm version Build Status Coverage Status

A Promise.race implementation that works with JavaScript map.

Background

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 promise-race-map

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 promise-race-map

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()
}

Contributions

Like us? Star us.

Want to make it better? File us an issue.

Don't like something you see? Submit a pull request.

Readme

Keywords

Package Sidebar

Install

npm i promise-race-map

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

9.31 kB

Total Files

10

Last publish

Collaborators

  • compulim