简体   繁体   English

蓝鸟未处理的错误与发出NodeJS

[英]Bluebird unhandled Error with emit NodeJS

im using Bluebird v2.8.2 Promises, but im getting unhandled error and code in catch is not processed. 我使用Bluebird v2.8.2 Promise,但是我收到未处理的错误,并且未处理catch中的代码。

EDITED to respones: 编辑回复:

var Promise = require('bluebird')
    util = require('util')
    EventEmitter = require('events').EventEmitter;

var Module = function() {
    EventEmitter.call(this);
};

util.inherits(Module, EventEmitter);

var getData = function() {
    return Promise.reject(new Error('test'));
};


Module.prototype.getCustomer = function() {
    var self = this;        

    setTimeout(function() {
        getData().then(function() {})
            .catch(function(error) {
                self.emit('error', error); // This causes problem!!
            });
    }, 1000);
}

SOLVED: Last comment of Esailija 已解决: Esailija的最新评论

It is processed just fine, however most likely your code inside the catch handler had a bug and threw an error. 可以很好地处理它,但是在catch处理程序中的代码很可能出现了错误并引发了错误。 I suggest you read the printed unhandled error more carefully to see what it is. 建议您更仔细地阅读未处理的错误,以查看错误原因。

The equivalent synchronous code would behave same way: 等效的同步代码的行为方式相同:

   try {
        throw new Error("test");
   } catch (e) {
        causesUnhandledReferenceError;
   }

If you wanted to catch that error (You certainly don't, just for exercise) you would need another try catch: 如果您想捕获该错误(您肯定不是,只是为了锻炼),则需要另一个try catch:

try {
    throw new Error("test");
} catch (e) {
    try {
        doesntCauseUnhandledReferenceError;
    } catch (e) {
        // logs reference error
        console.log(e)
    }
}

Similarly with promises: 与承诺类似:

getData().then(function() {

}).catch(function(e) {
    doesntCauseUnhandledReferenceError;
}).catch(function(e) {
      // logs reference error
      console.log(e)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM