Socket
Socket
Sign inDemoInstall

p-wait-all

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-wait-all

`Promise.all`, but it waits for all promises to settle even if one of them rejected


Version published
Maintainers
1
Created
Source

p-wait-all

Promise.all, but it waits for all promises to settle even if one of them rejected.

Installation

With npm:

npm install --save p-wait-all

Usage

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:

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):

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

Keywords

FAQs

Package last updated on 13 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc