-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathAbortablePromise.js
86 lines (73 loc) · 2.07 KB
/
AbortablePromise.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var Promise = require('./Promise');
function makeAbortable(promise, abort) {
promise.abort = abort;
// Hijack promise.then so it returns an abortable promise.
var _then = promise.then;
promise.then = function () {
return makeAbortable(_then.apply(promise, arguments), abort);
};
return promise;
}
/**
* A Promise class with an abort() method that calls the onAbort function
* provided by the resolver.
*
* Example:
*
* var promise = new AbortablePromise(function (resolve, reject, onAbort) {
* // Use resolve & reject as you normally would.
* var request = makeRequest( ... , function (error, response) {
* if (error) {
* reject(error);
* } else {
* resolve(response);
* }
* });
*
* // Use onAbort to register a promise.abort() function. It is the
* // responsibility of this function to abort the execution of the
* // promise and resolve/reject as needed.
* onAbort(function () {
* request.abort();
* reject(new Error('Request was aborted'));
* });
* });
*
* promise.abort(); // Calls the onAbort handler.
*/
function AbortablePromise(resolver) {
if (typeof resolver !== 'function')
throw new Error('AbortablePromise needs a resolver function');
var abort;
var promise = new Promise(function (resolve, reject) {
var aborter;
abort = function () {
if (aborter == null)
return;
var fn = aborter;
aborter = null;
try {
return fn.apply(this, arguments);
} catch (error) {
reject(error);
}
};
resolver(function (child) {
if (child && typeof child.abort === 'function') {
aborter = child.abort;
} else {
aborter = null;
}
resolve.apply(this, arguments);
}, function () {
aborter = null;
reject.apply(this, arguments);
}, function (fn) {
if (typeof fn !== 'function')
throw new Error('onAbort needs a function');
aborter = fn;
});
});
return makeAbortable(promise, abort);
}
module.exports = AbortablePromise;