简体   繁体   English

Bluebird 承诺和领域

[英]Bluebird Promises and Domains

I have a issue with throwing an error inside of a bluebird promise.我在蓝鸟承诺中抛出错误时遇到问题。 Take the following code:取以下代码:

var Promise = require('bluebird');
var domain = require('domain');

var problem = function() {
    return new Promise(function(resolve, reject){
        reject(new Error('Oops!'));
    });
};

domain.create()
    .on('error', function(e){
        console.log("Caught Error " + e.message)
        process.exit(1);
    })
    .run(function() {
        problem().done();
    });

I would expect to see Caught Error Oops!我希望看到Caught Error Oops! in the console.在控制台中。 However it seems that the error is not caught inside of the domain and i'm seeing a fatal error and stack trace in the console.但是,该错误似乎未在域内捕获,并且我在控制台中看到了致命错误和堆栈跟踪。

Does anyone know why?有谁知道为什么?

The exception is actually being handled by Bluebird.该异常实际上是由 Bluebird 处理的。 Promise rejections are not the same as unhandled exceptions. Promise 拒绝与未处理的异常不同。 If instead you create a real unhandled exception inside a setTimeout , which would therefore not be handled by Bluebird as it would be on a different stack, your domain will work as expected.相反,如果您在setTimeout创建了一个真正未处理的异常,因此 Bluebird 不会像在不同的堆栈上那样处理该异常,那么您的域将按预期工作。

var Promise = require('bluebird');
var domain = require('domain');

var problem = function() {
    return new Promise(function(resolve, reject){
        setTimeout(function() {
            throw new Error('boom!');
        }, 1)

    });
};

domain.create()
    .on('error', function(e){
        console.log("Caught Error " + e.message)
        process.exit(1);
    })
    .run(function() {
        problem().done();
    });

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

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