Handle error from setTimeout

后端 未结 4 646
孤城傲影
孤城傲影 2020-12-02 15:41

Simple question about try-catch for function in setTimeout

try {
    setTimeout(function () {
        throw new Error(\'error!\');
    }, 300)
} catch (e) {
         


        
相关标签:
4条回答
  • 2020-12-02 16:12

    Because the catch block lexically surrounds the setTimeout call but that is not the function that throws. The direct translation, is

    setTimeout(function () {
      try {
        throw new Error('error!');
      } catch (e) {
        console.log('eeee!');
        console.log(e);
      }
    }, 300);
    
    0 讨论(0)
  • 2020-12-02 16:18

    A bit strange solution, but sometimes it would be useful maybe...

    function globalErrorHandler(e) {
      console.warn('eeee!')
      console.warn(e);
    }
    
    const _setTimeoutOriginal = setTimeout;
    setTimeout = function(callback, timeout) {
      const args = Array.from(arguments).slice(2);
      _setTimeoutOriginal(function() {
        try {
          callback.apply(this, args);
        } catch (e) {
          globalErrorHandler(e);
        }
      }, timeout);
    };
    
    setTimeout(function() {
      throw new Error('error!');
    }, 300)

    0 讨论(0)
  • 2020-12-02 16:28

    Functions scheduled to run with setTimeout are executed in the main loop, outside the body of code that originated them.

    To handle errors, put the try-catch inside the setTimeout handler:

    setTimeout(function () {
      try {
        throw new Error('error!');
      } catch (e) {
        console.error(e);
      }
    }, 300)
    

    If you need to access the Error object from block that called setTimeout, use Promises:

    const promise = new Promise((resolve, reject) => {
      setTimeout(function () {
        try {
          throw new Error('error!');
        } catch (e) {
          reject(e)
        }
      }, 300)
    })
    
    promise
      .then(result => console.log("Ok " + result))
      .catch(error => console.error("Ouch " + error))
    

    This example above is not the most elegant way of handling the case with a Promise. Instead, implement a delay(ms) function like this:

    function delay(ms) {
      return new Promise(resolve => setTimeout(resolve, ms))
    }
    

    Then call

    delay(300).then(myFunction).catch(handleError)
    
    0 讨论(0)
  • 2020-12-02 16:28

    You can find good explanation in this Node.js official doc.

    The problem is that when the callback of your setTimeout() function executes the try { } catch(err) { } block is already exited. Also notice that the callback can crash Node.js process.

    However if you want to handle the errors in the callback of setTimeout() function, then you can listen them by using process global EventEmitter object

    process.on('uncaughtException', function(err){
      console.log(err)   
    })
    
    0 讨论(0)
提交回复
热议问题