Skip to content

Instantly share code, notes, and snippets.

@Dombo
Last active October 11, 2018 05:02
Show Gist options
  • Save Dombo/97edd861ff763eacd2a8d4779c6c3e2d to your computer and use it in GitHub Desktop.
Save Dombo/97edd861ff763eacd2a8d4779c6c3e2d to your computer and use it in GitHub Desktop.
Handle a collection of asynchronous actions, serially & blocking, with a consistent API regardless of what happens during execution.
# <3 leon
const Objects = [{}, {}, {}];
const PromiseBuilders = Objects.map(o => () => promise(o))
const serial = ([first, ..rest], results = []) =>
first === undefined
? Promise.resolve(results)
: first()
.then(res => serial(rest, results.concat(res)))
.catch(err => serial(rest, results.concat(err)))
const promise = Promise.resolve('foo')
.then(() => { throw 'foo_2' })
.catch(err => { throw err })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment