New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

every-promise

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

every-promise

Javascript needed a Promise.every(). Now we have one.

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

promise-every

Currently javascript does not have a Promise.every() function, so here it is.

1. You have an iterable of Promises.

2. You want to do something when they are all done.

3. You want all the data.

Code

Promise.every = require('every-promise');
Promise.every(ARRAY_OF_PROMISES, function(finished) {
  console.log(finished) // array of finished promises
});

Thats it!

If you want to access just the resolved or rejected promises the callback is passed three parameteres finished, resolved, rejected. In that order, finished will contain both resolved and rejected promises in the order you provided them in.

Instalation

npm install every-promise

You can also just copypaste this into your code:

Promise.every = function(promises, callback) {
  var len;
  const preserved = new Array((len = promises.length));
  const resolved = [], rejected = [];
  (function recurs(found){
    promises[found].then(good => {
      preserved[found] = resolved.push(good) && good;
      if(++found === len) callback(preserved, resolved, rejected);
      else recurs(found)
    }, bad => {
      preserved[found] = rejected.push(bad) && bad;
      if(++found === len) callback(preserved, resolved, rejected);
      else recurs(found)
    })
  })(0)
};

Or useing the traditional .then().catch().

Promise.every = function(promises, callback) {
  var len;
  const preserved = new Array((len = promises.length));
  const resolved = [], rejected = [];
  (function recurs(found){
    promises[found].then(good => {
      preserved[found] = good;
      resolved.push(good);
      if(++found === len) callback(preserved, resolved, rejected);
      else recurs(found)
    }).catch(bad => {
      preserved[found] = bad;
      rejected.push(bad)
      if(++found === len) callback(preserved, resolved, rejected);
      else recurs(found)
   })
  })(0)
};

Dependencies

None.

Keywords

FAQs

Package last updated on 08 Aug 2017

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