Created
September 28, 2016 09:29
-
-
Save adam-cowley/14362130987a28450729f1a751864e19 to your computer and use it in GitHub Desktop.
Run an array of promises one by one
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
function queue(all) { | |
return new Promise((resolve, reject) => { | |
const output = []; | |
next(all.splice(0, 1)) | |
function next(go) { | |
go(); | |
.then(res => { | |
output.push(res); | |
if ( !next ) { | |
return resolve(output) | |
} | |
next(all.splice(0,1)); | |
}) | |
} | |
}) | |
} | |
const output = queue([ | |
() => { return Promise.resolve(1) }, | |
() => { return Promise.resolve(2) }, | |
() => { return Promise.resolve(3) }, | |
]) | |
console.log(output) // [1,2,3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment