Skip to content

Instantly share code, notes, and snippets.

@othree
Last active August 29, 2015 14:17
Show Gist options
  • Save othree/b08b3272bfdfe081cb3b to your computer and use it in GitHub Desktop.
Save othree/b08b3272bfdfe081cb3b to your computer and use it in GitHub Desktop.
var T = function (id) {
var p = new Promise(function (resolve) {
setTimeout(function () {
console.log(id, 'resolved');
resolve(id);
}, Math.random()*3000);
});
return p;
};
var arr = [T('1'), T('2'), T('3'), T('4'), T('5')];
var f = function (id) {
console.log(id, 'parsed');
};
var sequential = function (f) {
return function (prev, curr) {
var p = Promise.all([curr, prev]);
p.then(function (args) { f(args[0]); });
return p.then(function (args) {
return args[1].concat(args[0]);
});
};
};
/*var sequential2 = function (f) {
return function (prev, curr) {
return Promise.all([curr, prev]).then(function (args) {
f(args[0]);
return args[1].concat(args[0]);
});
};
};*/
var all = arr.reduce(sequential(f), Promise.resolve([]));
all.then(function () {
console.log('all done');
console.log(arguments);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment