两个捕获的承诺/块声明都没有执行

Neither catch statement of promise/block is executing

本文关键字:声明 执行 承诺 两个      更新时间:2023-09-26

我在worker中有这段代码。

var cBtnStore = {
    a: function(){}
};

现在请注意,那里没有b键。

然后,我在我的工人中有一个功能。它这样做:

function run() {
    ...
    self.postMessageWithCallback(function() {
        cBtnStore.b(); ////// there is no b key, this should prevent going to the return
    });
    ...
    return promise; // returns a promise
}
var gCB = {};
self.postMessageWithCallback = function(aMsgArr, aCB) {
      var gCBID++;
      aMsgArr.push(gCBID);
      gCB[gCBID] = aCB;
      self.postMessage(aMsgArr);
}
self.onmessage = function(msg) {
    var aCBID = msg.data.pop();
    gCB[aCBID].apply(msg.data);
};

然后我在工人中执行此操作:

try {
    run().then(x=>{}, y=>{}).catch(z=>{console.error('promise caught')});
} catch(err) {
     console.log('runComplete threw');
}
console.log('result:', result);

实际发生的情况 - 这将记录以控制台runComplete()的返回值,该值"result:" [Promise object]catch 语句和 .catch 语句都不会执行。

我所期待的事情应该已经发生 - 它应该触发承诺或try-catch块的catch声明。

我的问题 - 有没有办法抓住这个?

当你写的时候

run().then(x=>{}, y=>{}).catch(z=>{console.error('promise caught')});

你的论点实际上意味着y=>{}"吃掉所有错误"。如果你想看到错误,你应该写

run().then(x=>{}).catch(z=>{console.error('promise caught')});