Skip to content

Commit

Permalink
Remove promises
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 21, 2010
1 parent 7a51184 commit 0485cc4
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 306 deletions.
95 changes: 0 additions & 95 deletions doc/api.txt
Expand Up @@ -275,101 +275,6 @@ manipulated, e.g. to remove listeners.
+emitter.emit(event, arg1, arg2, ...)+ ::
Execute each of the listeners in order with the supplied arguments.

=== +events.Promise+

+require("events")+ to access the events module.

+events.Promise+ inherits from +process.EventEmitter+. A promise emits one of two
events: +"success"+ or +"error"+. After emitting its event, it will not
emit anymore events.

[cols="1,2,10",options="header"]
|=========================================================
| Event | Parameters | Notes
| +"success"+ | (depends) |
| +"error"+ | (depends) |
|=========================================================

+promise.addCallback(listener)+ ::
Adds a listener for the +"success"+ event. Returns the same promise object.
The listener is executed right away if the promise has already fired.

+promise.addErrback(listener)+ ::
Adds a listener for the +"error"+ event. Returns the same promise object.
The listener is executed right away if the promise has already fired.

+promise.emitSuccess(arg1, arg2, ...)+ ::
If you created the promise (by doing +new events.Promise()+) then call
+emitSuccess+ to emit the +"success"+ event with the given arguments.
+
(+promise.emit("success", arg1, arg2, ...)+ should also work, but doesn't at
the moment due to a bug; use +emitSuccess+ instead.)

+promise.emitError(arg1, arg2, ...)+ ::
Emits the +"error"+ event. If no error handler is attached to the promise
between +promise.emitError()+ and +process.nextTick()+, an exception is
thrown.
+
To explain the exception behavior, assume you have a "computeQuestion"
function as follows:
+
----------------------------------------
var events = require('events');
function computeQuestion(answer) {
var promise = new events.Promise();
if (answer !== 42) {
promise.emitError('wrong answer');
return promise;
}
// compute the question for 42
return promise;
}
----------------------------------------
+
You can stop an exception to be thrown here by attaching an errback handler
right away (in the same event loop tick) like this:
+
----------------------------------------
computeQuestion(23).addErrback(function() {
// No exception will be thrown
});
----------------------------------------
+
However, if you try to attach the error handler in a later tick, the promise
will already have thrown an exception:
+
----------------------------------------
var promise = computeQuestion(23);
setTimeout(function() {
promise.addErrback(function() {
// This will never execute,
// the promise already threw an exception
});
}, 1000);
----------------------------------------

+promise.timeout(timeout = undefined)+ ::
If the +timeout+ parameter is provided, the promise will emit an +"error"+
event after the given amount of milliseconds. The timeout is canceled by any
+"success"+ or +"error"+ event being emitted by the promise.
+
To tell a timeout apart from a regular "error" event, use the following test:
+
----------------------------------------
promise.addErrback(function(e) {
if (e instanceof Error && e.message === "timeout") {
// handle timeout
} else {
// handle regular error
}
});
----------------------------------------
+
If the +timeout+ parameter is not provided, the current timeout value, if any,
is returned.



== Standard I/O

Expand Down
85 changes: 0 additions & 85 deletions src/node.js
Expand Up @@ -195,91 +195,6 @@ var eventsModule = createInternalModule('events', function (exports) {
if (!this._events.hasOwnProperty(type)) this._events[type] = [];
return this._events[type];
};

exports.Promise = function () {
exports.EventEmitter.call(this);
this._blocking = false;
this.hasFired = false;
this._values = undefined;
};
process.inherits(exports.Promise, exports.EventEmitter);

process.Promise = exports.Promise;

exports.Promise.prototype.timeout = function(timeout) {
if (!timeout) {
return this._timeoutDuration;
}

this._timeoutDuration = timeout;

if (this.hasFired) return;
this._clearTimeout();

var self = this;
this._timer = setTimeout(function() {
self._timer = null;
if (self.hasFired) {
return;
}

self.emitError(new Error('timeout'));
}, timeout);

return this;
};

exports.Promise.prototype._clearTimeout = function() {
if (!this._timer) return;

clearTimeout(this._timer);
this._timer = null;
}

exports.Promise.prototype.emitSuccess = function() {
if (this.hasFired) return;
this.hasFired = 'success';
this._clearTimeout();

this._values = Array.prototype.slice.call(arguments);
this.emit.apply(this, ['success'].concat(this._values));
};

exports.Promise.prototype.emitError = function() {
if (this.hasFired) return;
this.hasFired = 'error';
this._clearTimeout();

this._values = Array.prototype.slice.call(arguments);
this.emit.apply(this, ['error'].concat(this._values));

if (this.listeners('error').length == 0) {
var self = this;
process.nextTick(function() {
if (self.listeners('error').length == 0) {
throw (self._values[0] instanceof Error)
? self._values[0]
: new Error('Unhandled emitError: '+JSON.stringify(self._values));
}
});
}
};

exports.Promise.prototype.addCallback = function (listener) {
if (this.hasFired === 'success') {
listener.apply(this, this._values);
}

return this.addListener("success", listener);
};

exports.Promise.prototype.addErrback = function (listener) {
if (this.hasFired === 'error') {
listener.apply(this, this._values);
}

return this.addListener("error", listener);
};
});

var events = eventsModule.exports;
Expand Down
47 changes: 0 additions & 47 deletions test/mjsunit/test-promise-timeout.js

This file was deleted.

79 changes: 0 additions & 79 deletions test/mjsunit/test-promise.js

This file was deleted.

4 comments on commit 0485cc4

@qm3ster

This comment was marked as off-topic.

@addaleax
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qm3ster This commit happened more than 8 years ago, and Promises have long been re-introduced into Node.js since then. They are definitely going to be a main target for Node’s APIs in the future.

@qm3ster

This comment was marked as off-topic.

@joyeecheung
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qm3ster This commit was landed a long time ago in another repo. The discussion was likely to have happened in a lot of issues there as well as the now-rarely-used node.js mailling list. If you are interested in some archaeology you might want to dig in those places.

Whoever watches this repo gets hundreds of notifications each day. Discussing about a 8 year old commit here does not enable us to go back in time, and is likely to spam everybody with the same arguments happened 8 years ago.

Please sign in to comment.