Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If addErrback() is called before addCallback(), in case of emitError, both the errback and the callback are executed #1

Closed
pilif opened this issue Jul 2, 2010 · 1 comment

Comments

@pilif
Copy link

pilif commented Jul 2, 2010

let's assume this sample code

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();
@kriszyp
Copy link
Owner

kriszyp commented Jul 3, 2010

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.

kriszyp pushed a commit that referenced this issue Dec 13, 2012
Corrected deprecated uses of "sys."
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants