Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active May 7, 2019 21:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ORESoftware/58dce6edc8bb73632c30d137aef83f52 to your computer and use it in GitHub Desktop.
Run promises in series with an initial value
// promises are wrapped in a function - known as "promise-providers"
const providers = [
function(v){
return Promise.resolve(v+1);
},
function(v){
return Promise.resolve(v+2);
},
function(v){
return Promise.resolve(v+3);
}
];
const inSeries = function(providers, initialVal){
if(providers.length < 1){
return Promise.resolve(null);
}
return providers.reduce((a,b) => a.then(b), providers.shift()(initialVal));
};
inSeries(providers, 1).then(v => {
console.log(v);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment