forked from cujojs/when
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancelable.js
66 lines (54 loc) · 2.19 KB
/
cancelable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* cancelable.js
*
* Decorator that makes a deferred "cancelable". It adds a cancel() method that
* will call a special cancel handler function and then reject the deferred. The
* cancel handler can be used to do resource cleanup, or anything else that should
* be done before any other rejection handlers are executed.
*
* Usage:
*
* var cancelableDeferred = cancelable(when.defer(), myCancelHandler);
*
* @author [email protected]
*/
(function(define) {
define(['./when'], function(when) {
/**
* Makes deferred cancelable, adding a cancel() method.
*
* @param deferred {Deferred} the {@link Deferred} to make cancelable
* @param canceler {Function} cancel handler function to execute when this deferred is canceled. This
* is guaranteed to run before all other rejection handlers. The canceler will NOT be executed if the
* deferred is rejected in the standard way, i.e. deferred.reject(). It ONLY executes if the deferred
* is canceled, i.e. deferred.cancel()
*
* @returns deferred, with an added cancel() method.
*/
return function(deferred, canceler) {
var delegate = when.defer();
// Add a cancel method to the deferred to reject the delegate
// with the special canceled indicator.
deferred.cancel = function() {
delegate.reject(canceler(deferred));
};
// Ensure that the original resolve, reject, and progress all forward
// to the delegate
deferred.then(delegate.resolve, delegate.reject, delegate.progress);
// Replace deferred's promise with the delegate promise
deferred.promise = delegate.promise;
// Also replace deferred.then to allow it to be called safely and
// observe the cancellation
deferred.then = delegate.promise.then;
return deferred;
};
});
})(typeof define == 'function'
? define
: function (deps, factory) { typeof module != 'undefined'
? (module.exports = factory(require('./when')))
: (this.when_cancelable = factory(this.when));
}
// Boilerplate for AMD, Node, and browser global
);