Handle error from setTimeout

后端 未结 4 667
孤城傲影
孤城傲影 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);
    

提交回复
热议问题