Here is an example of my code:

try { //Тут другой код который может вызвать ошибку setTimeout(()=>{ throw new Error('Тестовая ошибка!'); },1); } catch (e) { alert('Ошибка: '+e.name + ":" + e.message); } 

How to catch the error caused in the setTimeout handler?

  • wrap in try ... catch the body of the function in setTimeout. Another way - Grundy
  • in other words, drag try-catch inside - Pavel Mayorov
  • Are you interested in this particular construct or how do you handle errors in asynchronous code? - Duck Learns to Take Cover
  • @ Duck Learn, Mind hint at Promise? - Grundy
  • one
    @Mr_Epic, well, quite often it is done with the help of a reject kollbek inside Promise if in short - Duck Learns to Take Cover

0