Skip to content

Instantly share code, notes, and snippets.

@adam-cowley
Created September 28, 2016 09:29
Show Gist options
  • Save adam-cowley/14362130987a28450729f1a751864e19 to your computer and use it in GitHub Desktop.
Save adam-cowley/14362130987a28450729f1a751864e19 to your computer and use it in GitHub Desktop.
Run an array of promises one by one
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