Why does this promise silently fail?

后端 未结 1 1525
误落风尘
误落风尘 2021-01-23 00:50

db.collection.findOne is an async operation (MongoDB, but that doesn\'t really matter here), which is why I\'m wrapping it in a promise here.

var l         


        
1条回答
  •  粉色の甜心
    2021-01-23 01:30

    It is not swallowing that error, but if a then or catch handler results in an error, then the current promise will be rejected with that error.

    In your case, ReferenceError is thrown, but it rejects the promise. You can see the actual error being propagated, by attaching a catch handler, like this

    new Promise(function (resolve, reject) {
        resolve(true);
      })
      .then(function (result) {
        console.log(result, bar);
      })
      .catch(function (er) {
        console.error('Inside Catch', er);
      });
    

    Now you will see

    Inside Catch [ReferenceError: bar is not defined]
    

    Further Reading:

    1. Why cannot I throw inside a Promise.catch handler?

    0 讨论(0)
提交回复
热议问题