2016-05-16 6 views
2

私はnodejsを初めて使って、最初に大きなプロジェクトを作成しようとしています。残念ながら、私はQのフルフィルメントハンドル内でミスをしたときにnodejsがエラーなしで終了します。nodejs + Q約束:フルフィルメントハンドルの参照例外がありません

例:

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"); 

'

出力は次のようになりますので、 "imnotexisting" 部分のない構文/参照または任意の他のエラーと

Start 
Stop 

。 フルフィルメントハンドル以外の同じエラーが、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セクションを追加する必要があります:

+0

nodejsにも同じ6.1.0 –

+0

すべてのヘルプやコメントはありますか?この問題は、プロジェクトがかなり複雑になっているので、多くのアシンクパスと長いループがあるため、すべての誤植がクリティカルになります。アプリ内の実行パスの一部にエラーメッセージが表示されずに失敗します。それらを追跡するには年齢がかかる... –

答えて

0

OK、私はこのことがわかりました

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:存在しないことが定義されていません