Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Remove Promise.prototype.wait()
Browse files Browse the repository at this point in the history
I don't want users to have to think about coroutine safety.

http://thread.gmane.org/gmane.comp.lang.javascript.nodejs/2468/focus=2603
  • Loading branch information
ry committed Feb 19, 2010
1 parent 860d008 commit 7647835
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 207 deletions.
66 changes: 0 additions & 66 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,72 +281,6 @@ var eventsModule = createInternalModule('events', function (exports) {

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

/* Poor Man's coroutines */
var coroutineStack = [];

exports.Promise.prototype._destack = function () {
this._blocking = false;

while (coroutineStack.length > 0 &&
!coroutineStack[coroutineStack.length-1]._blocking)
{
coroutineStack.pop();
process.unloop("one");
}
};

exports.Promise.prototype.wait = function () {
var self = this;
var ret;
var hadError = false;

if (this.hasFired) {
ret = (this._values.length == 1)
? this._values[0]
: this.values;

if (this.hasFired == 'success') {
return ret;
} else if (this.hasFired == 'error') {
throw ret;
}
}

self.addCallback(function () {
if (arguments.length == 1) {
ret = arguments[0];
} else if (arguments.length > 1) {
ret = Array.prototype.slice.call(arguments);
}
self._destack();
});

self.addErrback(function (arg) {
hadError = true;
ret = arg;
self._destack();
});

coroutineStack.push(self);
if (coroutineStack.length > 10) {
process.stdio.writeError("WARNING: promise.wait() is being called too often.\n");
}
self._blocking = true;

process.loop();

process.assert(self._blocking == false);

if (hadError) {
if (ret) {
throw ret;
} else {
throw new Error("Promise completed with error (No arguments given.)");
}
}
return ret;
};
});

var events = eventsModule.exports;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* XXX Can this test be modified to not call the now-removed wait()? */

process.mixin(require("./common"));


puts('first stat ...');

fs.stat(__filename)
Expand All @@ -13,4 +16,4 @@ fs.stat(__filename)
})
.addErrback(function() {
throw new Exception();
});
});
11 changes: 1 addition & 10 deletions test/mjsunit/test-promise-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ setTimeout(function() {
promise.emitSuccess('Am I too late?');
}, 500);

var waitPromise = new events.Promise();
try {
waitPromise.timeout(250).wait()
} catch (e) {
assert.equal(true, e instanceof Error);
assert.equal('timeout', e.message);
timeouts++;
}

var successPromise = new events.Promise();
successPromise.timeout(500);
setTimeout(function() {
Expand All @@ -52,5 +43,5 @@ errorPromise.addErrback(function(e) {
});

process.addListener('exit', function() {
assert.equal(2, timeouts);
assert.equal(1, timeouts);
});
98 changes: 0 additions & 98 deletions test/mjsunit/test-promise-wait.js

This file was deleted.

32 changes: 0 additions & 32 deletions test/mjsunit/test-wait-ordering.js

This file was deleted.

0 comments on commit 7647835

Please sign in to comment.