Created
September 15, 2017 19:51
-
-
Save xeaone/4d33ff75db2355a12e19907d56d34bfe to your computer and use it in GitHub Desktop.
Promise Async Each
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
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