【问题标题】:Get error with try/catch in Sequencial async functions在顺序异步函数中使用 try/catch 出错
【发布时间】:2019-07-05 13:37:31
【问题描述】:

我有一系列可以解决或拒绝的异步函数。此功能必须以正确的顺序执行,并且相互依赖。所以,我使用了 3 个异步函数和一个 try catch 块。问题是当我拒绝异步函数中的某些承诺时,catch 块不会收到拒绝回调发送的错误。如何获得拒绝发送的错误? (代码如下)

或者我应该使用承诺链?我想避免这种情况...

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)

        }


    })


}

}

【问题讨论】:

标签: javascript


【解决方案1】:

我通常是这样来的。

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()

【讨论】:

    【解决方案2】:

    您不需要try/catch。在等待方法时,您应该使用承诺 .catch

    你也不需要return new Promise (...)

    如果您的承诺被拒绝,它将进入该承诺的.catch,这与常规的catch 不同。

    你可以只返回嵌套的 Promise。

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

    【讨论】:

    • @Bergi 这就是为什么我添加到我的问题But I also don't think you need to return new Promise(...). 我还进行了编辑以使其更清楚以及如何不使用return new Promise(...).
    • 谢谢。请在第一个 sn-p 中修复它。使用.catch(reject) 并没有真正起作用,因为它只是继续执行异步函数。
    • @Bergi 谢谢,刚刚编辑了问题以使其更清晰,更易于理解
    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 2020-01-02
    • 2011-09-16
    • 2021-07-13
    • 1970-01-01
    • 2018-12-20
    • 2017-02-06
    • 2018-09-03
    相关资源
    最近更新 更多