Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-series-node

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-series-node

Execute methods that return promises in series

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
56
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

Promise Series

Execute array of methods that return promises, in series.

Installation

$ npm install promise-series

Basic Usage

var promiseSeries = require('promise-series');

var func1 = function() {
  return new Promise(function(resolve, reject) {
    resolve('hello');
  });
};

var func2 = function() {
  return new Promise(function(resolve, reject) {
    resolve('world');
  });
};

promiseSeries([func1, func2]).then( (results) => {
  console.log(results);
});

This will print:

['hello', 'world']  //results are returned in the order they were executed

Halt condition

Optionally, you make choose to provide a callback that is run against each result. If the test fails, the subsequent functions in the series will not be executed, and the series will resolve immediately.

var func1 = function() {
  return new Promise(function(resolve, reject) {
    resolve(true);
  });
};

var func2 = function() {
  return new Promise(function(resolve, reject) {
    resolve(false);
  });
};

var func3 = function() {
  return new Promise(function(resolve, reject) {
    resolve(true);
  });
};

promiseSeries([func1, func2, func3], function(res) {
  return res === true; //only promises that resolve(true) pass the test
}).then( (data) => {
  console.log(results);
});

This will print:

//note that func3 is not included, because func2 failed before it ran
//also note that results include the failed result
[true, false] 

Non-standard inputs

If a function does not return a promise, the return value will be passed through to the results:

var nonPromiseFunc = function() {
  return 'cruel';
};

promiseSeries([func1, nonPromiseFunc, func2]).then( (data) => {
  console.log(results);
});

This will print:

['hello', 'cruel', 'world']

If one of the inputs is not a function, the input will be passed through to the results:

promiseSeries([func1, 'foo', 42, func2]).then( (data) => {
  console.log(results);
});

This will print:

['hello', 'foo', 42, 'world']

Keywords

FAQs

Package last updated on 26 Mar 2015

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