简体   繁体   English

在Sequencial异步函数中尝试/捕获错误

[英]Get error with try/catch in Sequencial async functions

I have a sequence of async functions that can be resolved or rejected. 我有一系列异步函数可以解决或拒绝。 This functions must be executed in the correct sequence and depend one on other. 此功能必须以正确的顺序执行,并且彼此依赖。 So, i used 3 async functions and a try catch block. 因此,我使用了3个异步功能和一个try catch块。 The problem is that when I reject some of the promises in the async functions, the catch block dont get the error sended on the reject callback. 问题是,当我拒绝异步函数中的某些promise时,catch块不会得到在拒绝回调上发送的错误。 How can I get the error sended on reject? 如何获得拒绝时发送的错误? (code bellow) (下面的代码)

Or should I use promise chaining? 还是应该使用诺言链? I would like to avoid this... 我想避免这种情况...

const methods = {

methodOne: async output => {

    return new Promise( (resolve, reject) => {

        if(error)
            reject(error) // want to get this error in try/catch block
        else 
            resolve()

        })

    })

},

methodTwo: async () => {

    return new Promise( (resolve, reject) => {

        if(error)
            reject(error)
        else 
            resolve('output')

        })

    })

},

methodThree: async () => {

    return new Promise( (resolve, reject) => {

        if(error)
            reject(error)
        else 
            resolve()

        })

    })

},

runMethods: async () => {

    return new Promise( async (resolve, reject) => {

        try {

            await methods.methodOne()
            const output = await methods.methodTwo()
            await methods.methodThree(output)
            resolve()


        } catch(error) {

            console.log(error)
            reject(error)

        }


    })


}

}

You don't need the try/catch . 您不需要try/catch You should use the promise .catch when awaiting for the methods. 等待方法时,应使用promise .catch

You also don't need to return new Promise (...) . 您也不需要return new Promise (...)

If your promise is rejected, it will go in the .catch of that promise, wich is different from regular catch . 如果您的承诺被拒绝,它将进入该承诺的.catch中,这与常规catch不同。

You could just return nested promises. 您可以返回嵌套的承诺。

runMethods: () => {    
    return methods.methodOne()
            .then(() => methods.methodTwo())
            .then(output  => methods.methodThree(output))                     
}

I usually come at it this way. 我通常是这样来的。

 const methods = { methodOne: () => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('method1') resolve() },1000) }).catch(e=>console.log(e)) }, methodTwo: () => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('method2') resolve() }, 1000) }).catch(e => console.log(e)) }, methodThree: () => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('method3') reject('whoops something happened') }, 1000) }).catch(e => console.log(e)) }, runMethods: () => { let run = new Promise(resolve=>resolve(true)) run .then(result1 => methods.methodOne()) .then(result2 => methods.methodTwo()) .then(result3 => methods.methodThree()) .catch(e=>console.log(e)) } } methods.runMethods() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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