
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
#pseries pseries is a JavaScript micro-library for easily executing asynchronous functions in series. You pass in an array of promise-returning functions and it executes them in series then returns a promise.
##Installation
npm install pseries
(if using version 1.x.x, requires --harmony_generators
flag to be set)
##Benefits
##Example using ES2015
var myFuncs = [ // an example array of promise-returning functions
//run this function first
() => new Promise((resolve, reject) => {
someAsyncFunc((err, res) => {
if(err) reject(err);
else resolve(res);
});
}),
//then run this function...
() => new Promise((resolve, reject) => {
anotherAsyncFunc((err, res) => {
if(err) reject(err);
else resolve(res);
});
}),
];
// pass that array into pseries and let it do the work
pseries(myFuncs).then(
res => {
// do something with the response (an array of the responses from each of the functions)
},
err => {
// handle any error
}
);
##Examples
var myFuncs = [ // an example array of promise-returning functions
function() { //run this function first
return new Promise(function(resolve, reject) {
someAsyncFunc(function(err, res) {
if(err) {
reject(err);
}
resolve(res);
});
});
},
function() { //then run this function...
return new Promise(function(resolve, reject) {
anotherAsyncFunc(function(err, res) {
if(err) {
reject(err);
}
resolve(res);
});
});
}
];
// pass that array into pseries and let it do the work
pseries(myFuncs).then(
function(res) {
// do something with the response (an array of the responses from each of the functions)
},
function(err) {
// handle any error
}
);
or
pseries([
function() {
return new Promise(function(resolve, reject) {
someAsyncFunc(function(err, res) {
if(err) {
reject(err);
}
resolve(res);
});
});
},
function() {
return new Promise(function(resolve, reject) {
anotherAsyncFunc(function(err, res) {
if(err) {
reject(err);
}
resolve(res);
});
});
}
]).then(
function(res) {
// do something with the response
},
function(err) {
// handle any error
}
);
You can also nest pseries calls.
pseries([
firstFunction,
function() {
return new Promise(function(resolve, reject) {
pseries([
someFunction,
anotherFunction
]).then(
function(res) {
resolve(res);
},
function(err) {
reject(err);
}
);
});
},
thirdFunction
]).then(
function(res) {
// do something with the response
},
function(err) {
// handle any error
}
);
This is much cleaner than the following, traditional way of handling asynchronous functions.
// Don't do this!!!
var myFunc = function() {
someAsyncFunc(function(err, res) {
if(err) {
// handle error
} else {
// do something with response
anotherAsyncFunc(function(err, res) {
if(err) {
// handle error... again!
} else {
//do something with response
aThirdAsyncFunc(function(err, res) {
if(err) {
// handle error a third time!
} else {
//do something with response
...
}
});
}
});
}
});
};
##License Apache License Version 2.0
Copyright (c) 2016 by Ryan Burgett.
FAQs
Execute promise-returning functions in series.
We found that pseries demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.