Created
February 10, 2016 22:49
-
-
Save jondejong/0b499a20cfb17176afb4 to your computer and use it in GitHub Desktop.
A simple, if ugly, approach to promise chaining
This file contains 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
var things = [ | |
//...Array of things to process... | |
]; | |
var methodThatReturnsPromise() = function { | |
// Must be executed on all the things | |
// In order | |
// return a promise | |
}; | |
var i = 0; | |
var whenDone = function () { | |
//We're done now, move on... | |
}; | |
var proccessThing = function (thing) { | |
return methodThatReturnsPromise(thing).then(function () { | |
i++; | |
if (i < thing.length) { | |
proccessThing(things[i]); | |
} else { | |
whenDone(); | |
} | |
}); | |
}; | |
proccessThing(things[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment