forked from cujojs/when
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeout.js
71 lines (61 loc) · 1.86 KB
/
timeout.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
/** @license MIT License (c) copyright 2011-2013 original author or authors */
/**
* timeout.js
*
* Helper that returns a promise that rejects after a specified timeout,
* if not explicitly resolved or rejected before that.
*
* @author Brian Cavalier
* @author John Hann
*/
(function(define) {
define(function(require) {
/*global setTimeout,clearTimeout*/
var when, setTimer, cancelTimer, cjsRequire, vertx;
when = require('./when');
cjsRequire = require;
try {
vertx = cjsRequire('vertx');
setTimer = function (f, ms) { return vertx.setTimer(ms, f); };
cancelTimer = vertx.cancelTimer;
} catch (e) {
setTimer = setTimeout;
cancelTimer = clearTimeout;
}
/**
* Returns a new promise that will automatically reject after msec if
* the supplied trigger doesn't resolve or reject before that.
*
* @param {number} msec timeout in milliseconds
* @param {*|Promise} trigger any promise or value that should trigger the
* returned promise to resolve or reject before the msec timeout
* @returns {Promise} promise that will timeout after msec, or be
* equivalent to trigger if resolved/rejected before msec
*/
return function timeout(msec, trigger) {
// Support reversed, deprecated argument ordering
if(typeof trigger === 'number') {
var tmp = trigger;
trigger = msec;
msec = tmp;
}
return when.promise(function(resolve, reject, notify) {
var timeoutRef = setTimer(function onTimeout() {
reject(new Error('timed out after ' + msec + 'ms'));
}, msec);
when(trigger,
function onFulfill(value) {
cancelTimer(timeoutRef);
resolve(value);
},
function onReject(reason) {
cancelTimer(timeoutRef);
reject(reason);
},
notify
);
});
};
});
})(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });