You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var sys = require("sys");
var promise = require('./promise');
var p = new promise.Promise();
p.addErrback(function(){
sys.debug('Errback');
}).addCallback(function(){
sys.debug('Callback');
});
p.emitError();
I would expect only seing "Errback" written to stderr, but what I see is both "Errback", followed by "Callback". This is counter-intuitive and what's worse, if I exchange the order in which I call addCallback and addErrback, the behavior changes and I get the output I expect:
var sys = require("sys");
var promise = require('./promise');
var p = new promise.Promise();
p.addCallback(function(){
sys.debug('Callback');
}).addErrback(function(){
sys.debug('Errback');
});
p.emitError();
The text was updated successfully, but these errors were encountered:
The reason for this behavior is that the result of the error handler is being passed to the callback handler. The error handler effectively "catches" the error, and therefore the promise returned from addErrback resolves to successful state since addErrback's function does not throw an error. I realize that part of the confusion comes from the names addCallback and addErrback. These are poor names and exist to maintain some backwards compatibility with Node's (very broken and poorly designed) promises.
This might be better illustrated by seeing the synchronous equivalent:
try{
throw new Error(); // p.emitError(); acts like a thrown error
}catch(e){ // p.addErrback(function(){ acts like a catch statement
sys.debug('Errback');
}
sys.debug('Callback'); // p.addCallback(function(){ acts like the next statement after the completion of the previous action, and gets executed because the error has been caught.
let's assume this sample code
I would expect only seing "Errback" written to stderr, but what I see is both "Errback", followed by "Callback". This is counter-intuitive and what's worse, if I exchange the order in which I call addCallback and addErrback, the behavior changes and I get the output I expect:
The text was updated successfully, but these errors were encountered: