Rethrowing error in promise catch

前端 未结 6 1779
迷失自我
迷失自我 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:32

    You actually don't need to re throw it, just leave the Promise.catch empty otherwise it will consider as un handle the reject and then wrap the code in a try catch and it will catch the error automatically which is passing down.

    try{
      promise.then(function(result){
        //some code
      }).catch(function(error) {
        //no need for re throwing or any coding. but leave this as this otherwise it will consider as un handled
      });
    }catch(e){
      console.log(e);
      //error can handle in here
    }
    

提交回复
热议问题