Skip to content

compulim/promise-race-map

Repository files navigation

promise-race-map

A Promise.race implementation that works with JavaScript map.

npm version Build Status Coverage Status

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.

About

Promise.race implementation to works with a JavaScript map

Resources

License

Stars

Watchers

Forks

Packages

No packages published