Skip to content

Instantly share code, notes, and snippets.

@fwg
Created November 22, 2009 16:35
Show Gist options
  • Save fwg/240628 to your computer and use it in GitHub Desktop.
Save fwg/240628 to your computer and use it in GitHub Desktop.
diff --git a/src/node.js b/src/node.js
index 6882102..e9b5905 100644
--- a/src/node.js
+++ b/src/node.js
@@ -257,6 +257,9 @@ process.Promise.prototype.timeout = function(timeout) {
};
process.Promise.prototype.cancel = function() {
+ if(this._cancelled) return;
+ this._cancelled = true;
+
this._events['success'] = [];
this._events['error'] = [];
diff --git a/test/test-promise-cancel.js b/test/test-promise-cancel.js
new file mode 100644
index 0000000..2009875
--- /dev/null
+++ b/test/test-promise-cancel.js
@@ -0,0 +1,12 @@
+process.mixin(require('./common'));
+
+var promise = new process.Promise();
+var cancelled = false;
+promise.addCancelback(function(){
+ if(cancelled){
+ assertUnreachable("promise should not cancel more than once");
+ }
+ cancelled = true;
+});
+promise.cancel();
+promise.cancel();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment