-
-
Save lancetw/8bc228057623a93b6ac2e33c57ded364 to your computer and use it in GitHub Desktop.
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
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 = (task) => { | |
let ret = [] | |
return task.reduce((promised, task) => { | |
return promised.then(task).then(value => { | |
ret.push(value) | |
return Promise.resolve(ret) | |
}) | |
}, 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