Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs+;Q承诺:履行句柄中没有引用异常_Node.js_Promise_Nullreferenceexception - Fatal编程技术网

Node.js nodejs+;Q承诺:履行句柄中没有引用异常

Node.js nodejs+;Q承诺:履行句柄中没有引用异常,node.js,promise,nullreferenceexception,Node.js,Promise,Nullreferenceexception,我是nodejs的新手,试图写第一个更大的项目。不幸的是,当我在Q fullfilment句柄中犯了一个错误时,我被nodejs出口卡住了,没有任何错误 例如: var Q = require('q'); function test1() { var deferred = Q.defer(); deferred.resolve(); return(deferred.promise); } console.log("Start"); tes

我是nodejs的新手,试图写第一个更大的项目。不幸的是,当我在Q fullfilment句柄中犯了一个错误时,我被nodejs出口卡住了,没有任何错误

例如:

var Q = require('q');
    function test1() {
        var deferred = Q.defer();
        deferred.resolve();
        return(deferred.promise);
}

console.log("Start");
test1() 
.then (function(ret) {
    imnotexisting;   //this should be shown as Reference Exception
    console.log("OK");
}, function(err) {
    console.log("FAIL");
});
console.log("Stop");
"

输出将是:

Start
Stop
由于“imnotexisting”部分,没有语法/引用或任何其他错误。 在FullFilmentHandle之外发生的相同错误会引发erorr

我在Ubuntu上使用nodejs 4.4.4。

好的,我发现:

One sometimes-unintuive aspect of promises is that if you throw an exception in the fulfillment handler, it will not be caught by the error handler.
(...)
To see why this is, consider the parallel between promises and try/catch. We are try-ing to execute foo(): the error handler represents a catch for foo(), while the fulfillment handler represents code that happens after the try/catch block. That code then needs its own try/catch block.
我们需要添加.fail部分,如下所示:

    var Q = require('q');
    function test1() {
        var deferred = Q.defer();
        deferred.resolve();
        return(deferred.promise);
}

console.log("Start");
test1() 
.then (function(ret) {
    imnotexisting;
    console.log("OK");
}, function(err) {
    console.log("FAIL");
})
.fail (function(err) {
    console.log("Error: "+err);
});
console.log("Stop");
结果是: 开始 停止
错误:ReferenceError:imnoteExisting未定义

同样适用于nodejs 6.1.0任何帮助或注释?这个问题使得我所有的输入错误都变得至关重要——因为我的项目现在相当复杂——有许多aync路径和长循环——应用程序中的一些执行路径失败,没有错误消息。追踪他们需要很长时间。。。