Using Q.promises: how to catch an async throw?

前端 未结 1 910
自闭症患者
自闭症患者 2020-12-04 03:04

I\'m using Q for promises, but when setting up some tests I discover I see way in catching async errors thrown inside a function that returns a promise.

I tried to

相关标签:
1条回答
  • 2020-12-04 03:21

    The exception in the setTimeout is not related anyhow to the promises, you have to catch that yourself using a try-catch-block.

    Or you use Q.delay:

    function func(){
        return Q.delay(100).then(function(){
            throw new Error("async error");
        });
    }
    
    func()
    .then(console.log.bind(console, "success"))
    .fail(console.log.bind(console));
    
    0 讨论(0)
提交回复
热议问题