[ Web Proxy ]
URL:
Viewing: https://javascript.info/task/error-async [Back]  [Original]

Error in setTimeout
EN

We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

    Search on Javascript.info:
    Search in the tutorial:
    Light themeDark theme
    DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
    back to the lesson

    Error in setTimeout

    What do you think? Will the .catch trigger? Explain your answer.

    new Promise(function(resolve, reject) {
      setTimeout(() => {
        throw new Error("Whoops!");
      }, 1000);
    }).catch(alert);
    solution

    The answer is: no, it wont:

    new Promise(function(resolve, reject) {
      setTimeout(() => {
        throw new Error("Whoops!");
      }, 1000);
    }).catch(alert);

    As said in the chapter, theres an implicit try..catch around the function code. So all synchronous errors are handled.

    But here the error is generated not while the executor is running, but later. So the promise cant handle it.


    Web Proxy Viewer  |  New URL  |  Original Page