https://github.com/compulim/promise-race-map
Promise.race implementation to works with a JavaScript map
https://github.com/compulim/promise-race-map
Last synced: 4 days ago
JSON representation
Promise.race implementation to works with a JavaScript map
- Host: GitHub
- URL: https://github.com/compulim/promise-race-map
- Owner: compulim
- License: mit
- Created: 2018-11-25T02:29:50.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T06:16:41.000Z (over 2 years ago)
- Last Synced: 2025-04-19T19:22:53.674Z (about 1 month ago)
- Language: JavaScript
- Size: 1.15 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# promise-race-map
A [`Promise.race`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) implementation that works with JavaScript map.
[](https://badge.fury.io/js/promise-race-map) [](https://travis-ci.org/compulim/promise-race-map) [](https://coveralls.io/github/compulim/promise-race-map)
## Background
[`Promise.race`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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.
```js
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.
```js
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](https://github.com/compulim/promise-race-map/stargazers) us.
Want to make it better? [File](https://github.com/compulim/promise-race-map/issues) us an issue.
Don't like something you see? [Submit](https://github.com/compulim/promise-race-map/pulls) a pull request.