Skip to content

Instantly share code, notes, and snippets.

@xeaone
Created September 15, 2017 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeaone/4d33ff75db2355a12e19907d56d34bfe to your computer and use it in GitHub Desktop.
Save xeaone/4d33ff75db2355a12e19907d56d34bfe to your computer and use it in GitHub Desktop.
Promise Async Each
function each (condition, method, context, index) {
index = index === undefined ? 0 : index;
if (condition.call(context, index)) {
return Promise.resolve().then(function () {
return method.call(context, index);
}).then(each.bind(null, condition, method, context, index+1));
} else {
return Promise.resolve();
}
}
// console.log('before');
// each(function (i) { return i < 10; }, function (i) {
// console.log(i++);
// // throw 'oops';
// }).then(function () {
// console.log('actually after');
// }).catch(function (error) {
// console.log(error);
// });
// console.log('after but not');
// Promise.resolve().then(function () { console.log('random'); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment