Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/goto-bus-stop/p-wait-all
`Promise.all`, but it waits for all promises to settle even if one of them rejected
https://github.com/goto-bus-stop/p-wait-all
async-await async-functions await promise promises
Last synced: 27 days ago
JSON representation
`Promise.all`, but it waits for all promises to settle even if one of them rejected
- Host: GitHub
- URL: https://github.com/goto-bus-stop/p-wait-all
- Owner: goto-bus-stop
- License: mit
- Created: 2018-02-06T10:21:14.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-29T11:19:21.000Z (over 3 years ago)
- Last Synced: 2024-10-05T16:25:50.089Z (about 1 month ago)
- Topics: async-await, async-functions, await, promise, promises
- Language: JavaScript
- Size: 8.79 KB
- Stars: 15
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# p-wait-all
`Promise.all`, but it waits for all promises to settle even if one of them rejected.
## Installation
With npm:
```bash
npm install --save p-wait-all
```## Usage
```js
var waitAll = require('p-wait-all')var settled = false
var result = waitAll([
Promise.reject(new Error('operation failed')),
delay(200).then(function () { settled = true })
])result.catch(function (err) {
// rejected after 200ms;
// settled === true
})
```Compare doing this with `Promise.all`:
```js
var settled = false
var result = Promise.all([
Promise.reject(new Error('operation failed')),
delay(200).then(function () { settled = true })
])result.catch(function (err) {
// rejected immediately;
// settled === false
})
```This behaviour is useful if you are doing two async operations that may fail individually, so that both operations need to be rolled back.
For example, saving two related mongoose models, where one may fail (eg. from duplicate key errors):
```js
waitAll([
model1.save(),
relatedModel.save()
]).catch(function (err) {
var p = []
if (!model1.isNew) p.push(model1.remove())
if (!relatedModel.isNew) p.push(relatedModel.remove())
return Promise.all(p)
})
```You cannot remove a model if it is still in the process of being saved, so you have to wait for all the `save()` promises to resolve before attempting to roll them back.
## API
### result = require('p-wait-all')(promises)
Wait for `promises` to settle. If any of them errored, reject the `result` promise with the error. If all of them resolved, resolve `result` with an array of resolution values, in order.
## License
[MIT](./LICENSE)