Created
October 12, 2015 22:10
-
-
Save mkulke/5d4df51e325b5683e8f3 to your computer and use it in GitHub Desktop.
sequential es2015 promises
This file contains hidden or 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
'use strict'; | |
var _ = require('underscore')._; | |
function promiseFactory (n) { | |
return function () { | |
return new Promise((resolve) => { | |
setTimeout(_.partial(resolve, n), 1000); | |
}); | |
}; | |
} | |
function wrap (promiseFn) { | |
return function (results) { | |
return promiseFn() | |
.then(result => { | |
console.log(result); | |
results.push(result); | |
return results; | |
}); | |
}; | |
} | |
var wrappedFns = _.map(_.times(10, promiseFactory), wrap); | |
_.reduce(wrappedFns, (cur, next) => { | |
return cur.then(next); | |
}, Promise.resolve([])) | |
.then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment