Promise reject() causes “Uncaught (in promise)” warning

前端 未结 3 535
鱼传尺愫
鱼传尺愫 2020-12-03 00:27

Once a promise reject() callback is called, a warning message \"Uncaught (in promise)\" appears in the Chrome console. I can\'t wrap my head around the

3条回答
  •  再見小時候
    2020-12-03 01:13

    Even if you use Promises correctly: p.then(p1).catch(p2) you can still get an uncaught exception if your p2 function eventually throws an exception which you intend to catch using a mechanism like window.onerror. The reason is that the stack has already been unwound by the error handling done in the promise. To fix this, make sure that your error code (called by the reject function) does not throw an exception. It should simply return.

    It would be nice if the error handling code could detect that the stack has already been unwound (so your error call doesn't have to have a flag for this case), and if anyone knows how to do this easily I will edit this answer to include that explanation.

提交回复
热议问题