Rethrowing error in promise catch

前端 未结 6 1822
迷失自我
迷失自我 2021-01-30 02:59

I found the following code in a tutorial:

promise.then(function(result){
    //some code
}).catch(function(error) {
    throw(error);
});

I\'m

6条回答
  •  無奈伤痛
    2021-01-30 03:36

    So it sounds like your question is, "In the promise chain, what does the .catch() method do?"

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

    The throw statement "will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate."

    In the promise chain, the .then() method will return some type of data chunk. This return of the chunk will complete the promise. The successful return of the data completes the promise. You can think of the .catch() method in the same way. .catch() however will handle unsuccessful data retrieves. The throw statement completes the promise. Occasionally, you will see developers use .catch((err) => {console.log(err))} which would also complete the promise chain.

提交回复
热议问题