Skip to content

Instantly share code, notes, and snippets.

@caasi
Forked from lancetw/promise-sequence.js
Last active June 7, 2017 14:31
Show Gist options
  • Save caasi/5422aafb490d091e33f2c3b2433fcfc2 to your computer and use it in GitHub Desktop.
Save caasi/5422aafb490d091e33f2c3b2433fcfc2 to your computer and use it in GitHub Desktop.
const actions = [
() => Promise.resolve(0),
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3),
() => Promise.reject(new Error('4')),
() => Promise.resolve(5),
() => Promise.reject(new Error('6')),
() => Promise.reject(new Error('7'))
]
const process = (tasks) =>
tasks.reduce(
(promised, task) =>
promised.then(acc =>
task().then(value =>
[...acc, value]
)),
Promise.resolve([])
)
process(actions)
.then(() => console.log('做完了!'))
.catch((err) => console.error(err.message, '炸掉啦'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment