2 module.exports = function(Promise) {
3 var errors = require("./errors.js");
4 var async = require("./async.js");
5 var CancellationError = errors.CancellationError;
7 Promise.prototype._cancel = function (reason) {
8 if (!this.isCancellable()) return this;
10 var promiseToReject = this;
11 while ((parent = promiseToReject._cancellationParent) !== undefined &&
12 parent.isCancellable()) {
13 promiseToReject = parent;
15 this._unsetCancellable();
16 promiseToReject._target()._rejectCallback(reason, false, true);
19 Promise.prototype.cancel = function (reason) {
20 if (!this.isCancellable()) return this;
21 if (reason === undefined) reason = new CancellationError();
22 async.invokeLater(this._cancel, this, reason);
26 Promise.prototype.cancellable = function () {
27 if (this._cancellable()) return this;
28 async.enableTrampoline();
29 this._setCancellable();
30 this._cancellationParent = undefined;
34 Promise.prototype.uncancellable = function () {
35 var ret = this.then();
36 ret._unsetCancellable();
40 Promise.prototype.fork = function (didFulfill, didReject, didProgress) {
41 var ret = this._then(didFulfill, didReject, didProgress,
42 undefined, undefined);
44 ret._setCancellable();
45 ret._cancellationParent = undefined;