简体   繁体   中英

if an error is thrown at some point in a promise chain, is it automatically propagated all the way down the chain?

Say you have a promise chain like this:

asyncFunction()
.then((value) => {
    resolve(value)
})
.then((value) => {
    resolve(value)
})
.then((value) => {
    resolve(value)
})
.catch((error) => {
    reject(error)
})

If an error is thrown during one of those function calls, does that error automatically propagate all the way down to the catch at the end of the chain? Say the error is thrown at the beginning, during asyncFunction() , what are the exact mechanics of how it propagates? Is the error passed to the handler of each then ? Or does each then look for a second handler, not see one, and so pass the error to the next then ?

Yes, it will get propagated down until it hits the first catch . All then clauses should be skipped.

I noticed the use of resolve() and reject() avoid this, there's no need for the Promise constructor pattern in case like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM