【问题标题】:How to catch error in callback function JS?如何在回调函数 JS 中捕获错误?
【发布时间】:2021-07-26 13:09:33
【问题描述】:

我想在回调函数中捕获错误。

(async function(){
    function wait(ms){
        return new Promise(function(resolve, reject){
            setTimeout(resolve, ms);
        });
    }

    async function test(){
        while(true) {
            if (window.err){
                throw 'This is an error!';
            }
            await wait(500);
        }
    }

    
    try {
        console.log('running ...');

        window.err = false;
        setTimeout(function(){
            window.err = true;
        }, 1000);

        test();
    
        await wait(3000);
        console.log('end')
        console.log('done')
    }
    catch(err){
        // How do i catch error of "test" function in this
        console.log('end')
        console.log('error')
    }
})()

.................................................. ..................................................... ..................................................... .................................................................. 我如何在此捕获 test 函数的错误? 我的英语不好,请帮助我!谢谢!

【问题讨论】:

    标签: javascript asynchronous error-handling async-await callback


    【解决方案1】:

    test 是一个异步函数,如果你在没有等待的情况下调用它,错误将被忽略。

    await test()代替test()

    或者,如果您不想等待它而只是在发生错误时收到通知,您可以使用Promise.catch

    test().catch(err => {
      // Handle the error in some way
      console.error(err)
    }) 
    

    【讨论】:

    • 我知道,但我想运行一个像(多线程)这样的函数。每次都检查状态!
    • @nguyenphuong 那是不可能的。 JS 是单线程的。除非你使用网络工作者。
    • @nguyenphuong 你能提供更多细节吗?你想达到什么目的?我也许可以向您展示生成器。
    【解决方案2】:

    如果你想从测试函数中捕获错误,你可以这样尝试

    (async function(){
        function wait(ms){
            return new Promise(function(resolve, reject){
                setTimeout(resolve, ms);
            });
        }
    
        async function test(){
          return new Promise((resolve, reject)  => {
           try {
              while(true) {
                if (window.err){
                    throw 'This is an error!';
                }
                await wait(500);
                resolve();
              }
           } catch(err) {
                 reject(err) // here we are catching the error and throw it back to the caller 
           }
          });
        }
    
        
        try {
            console.log('running ...');
    
            window.err = false;
            setTimeout(function(){
                window.err = true;
            }, 1000);
    
           try {
             await test();
           } catch (err) {
             console.log(err) // here we get the error from the test
           }
    
        
            await wait(3000);
            console.log('end')
            console.log('done')
        }
        catch(err){
            // How do i catch error of "test" function in this
            console.log('end')
            console.log('error')
        }
    })()
    
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 1970-01-01
      • 2018-08-09
      • 2019-02-17
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      相关资源
      最近更新 更多