質問

The Kris Kowal's Q docs states that Q.onerror is invoked on unhandled exceptions.
I can't make it work:

var Q = require('q');
Q.longStackSupport = true;
var util = require('util');

Q.onerror=function(){
    console.log('Q.onerror::')
    console.log(util.inspect(arguments))
}

function get(){
    var def=Q.defer();
    def.resolve('resolved');    
    return def.promise;
}

get()
.then(function(val){
    console.log('ok:'+val)
    undefined._prop;  // i would expect this exception to be  
                      // forwarded to Q.onerror... but it doesn't
    console.log('not reachd')
});

outputs:

ok:resolved

I think I didn't understand well the use of Q.onerror I'd like to track unhandled exceptions (and possibly rejections too) with a nice stack trace

役に立ちましたか?

解決

Q does not track* unhandled rejections at the moment, so you have to explicitly tell it the chain has ended.

Q.onerror handles exceptions unhandled inside done clauses:

get()
.done(function(val){ // you can not chain this, this indicates the chain is done
    console.log('ok:'+val)
    undefined._prop;  // i would expect this exception to be  
                      // forwarded to Q.onerror... but it doesn't
    console.log('not reachd')
});

This is unlike libraries like Bluebird, that can figure out unhandled rejections on their own, or native promises in Firefox that use GC to detect unhandled rejections.

* (atm, an experimental feature was added to Q and then removed)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top