Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
sequence-as-promise
Advanced tools
Executes array of functions and promises as sequence and returns promise
It's zero dependency and lightweight function that allows execute array of functions and promises in sequence and returns Promise
Behavior very similar to Promise.all
, but all promises or functions executes in sequence.
Function executes promises with functions one by one and returns promise withresults array
this code
promise1.then(() => promise2.then(() => promise3.then(callback))).then(done);
equivalent to
const sequence = require('sequence-as-promise');
sequence([promise1, promise2, promise3, callback]).then(done);
with npm
npm i --save sequence-as-promise
with yarn
yarn add sequence-as-promise
We have array of functions with promises, and we need to execute all that functions in sequence
All these functions accepts two arguments, (prevResult, results) => {}
prevResult
the result of previous function or promise callresults
an array of results from previous functions or promises callsconst sequence = require('sequence-as-promise');
sequence([
Promise.resolve({status: true}),
(prevResult/*{status: true}*/, results) => {
return {moveCircleToMiddle: true};
},
(prevResult/*{moveCircleToMiddle: true}*/, results) => {
return {showGrayCircle: true};
},
(prevResult/*{showGrayCircle: true}*/, results) => {
return {showMicrophone: true};
}
]).then((results) => console.log('all done', results))
Most standard use case is fetch dependant data one by one
const sequence = require('sequence-as-promise');
sequence([
fetchUser(32),
(user) => {
if (user && user.id === 1) {
return fetchAdminUrls(user.id);
}
return fetchUserUrls(user.id);
},
(urls) => {//previous fetch resolved and passed as argument
return urls.map(makeLink)
}
]).then((results) => {
const [user, _, links] = results;
renderHTML(user, links);
});
Any function or promise in sequence can throw an error, so we need handle it
const sequence = require('sequence-as-promise');
sequence([
fetchUser(32),
(user) => {
if (user && user.id === 1) {
return fetchAdminUrls(user.id); //for instance this fetch throws server error
}
return fetchUserUrls(user.id);
},
(urls) => { //this will not be executed, because previous promise thorws error
return urls.map(makeLink);
}
]).then(
(results) => {
const [user, _, links] = results;
renderHTML(user, links);
},
(results) => {
const error = results.pop(); //last item in results always be an error
renderError(error);
}
);
But, if we need to call all that functions with primitive values between them (why not?).
const sequence = require('sequence-as-promise');
sequence([
() => {
return {moveCircleToMiddle: true};
},
100,
(prevResult/*100*/, results) => {
return {showGrayCircle: prevResult};
},
(prevResult/*{showGrayCircle: 100}*/, results) => {
return {showMicrophone: true};
},
500,
(prev, values) => { // prev == 500
return {moveCircleToTop: true};
}
]).then((results) => console.log('all done', results))
Or we have Promises in that array of functions.
const sequence = require('sequence-as-promise');
sequence([
() => new Promise((resolve, reject) => {
resolve({moveCircleToMiddle: true});
}),
() => {
return {showGrayCircle: true};
}
]).then(() => console.log('all done'))
FAQs
Executes array of functions and promises as sequence and returns promise
The npm package sequence-as-promise receives a total of 43 weekly downloads. As such, sequence-as-promise popularity was classified as not popular.
We found that sequence-as-promise 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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.