Skip to content

Instantly share code, notes, and snippets.

@kainage
Last active June 17, 2016 22:59
Show Gist options
  • Save kainage/39a52267edd359a284d8069359d3a092 to your computer and use it in GitHub Desktop.
Save kainage/39a52267edd359a284d8069359d3a092 to your computer and use it in GitHub Desktop.
# Resolves an arbitrary number of promises in order. Returns the last promise
# unresolved (or an empty promise if there were none) for chainability.
serialResolver = (promises) ->
promise = promises.shift()
switch
# Promise array was empty to begin with.
when promise is undefined
$q.when()
# For the last promise in the array, return it for chainability.
when promises.length is 0
promise
# Process a promise serially and call the next one.
else
promise.then ->
serialResolver(promises)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment