Last active
May 7, 2019 21:28
Star
You must be signed in to star a gist
Run promises in series with an initial value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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