简体   繁体   English

Node.js Bluebird-承诺错误返回为null

[英]Node.js Bluebird - promise error being returned as null

I have a series of promises where if any of them throw an error it bubbles up to the top promise - kind of like global error catching. 我有一系列的承诺,如果其中任何一个抛出错误,它都会冒出最高的承诺-有点像全局错误捕获。

Here's a simplified version of how I've got things setup: 这是我进行设置的简化版本:

function bar() {
    return new Promise(function(resolve,reject) {
        var err = new Error('This is an error');
        err.name = 'Permission';
        return reject(null,err);
    });
}

function foo() {
    return new Promise(function(resolve,reject) {
        bar()
            .then(function(results) {
                return resolve(results);
            })
            .then(null,function(error) {
                console.log('Error caught'); //This shows
                console.log(error); //outputs null
                return reject(error);
            });
    });
}

foo()
    .then(function(results) {
        console.log(results);
    })
    .then(null,function(error) {
        //this never gets reached
    });

For some reason although the error function runs the value of 'error' is null. 由于某些原因,尽管错误函数运行,但“错误”的值为null。 Any ideas on what would cause this? 有什么想法会导致这种情况吗?

Thanks! 谢谢!

The reject function takes only one argument, the reason. reject功能仅接受一个参数,即原因。 So, when you call: 因此,当您致电:

reject(null, err);

You are passing null as the rejection reason and the err argument is not used. 您将null作为拒绝原因,并且不使用err参数。 Change your code to this: 将代码更改为此:

reject(err);

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

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