-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
49 lines (44 loc) · 1.21 KB
/
index.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
// Copyright (c) 2015-2017 David M. Lee, II
'use strict';
/**
* Local reference to TimeoutError
* @private
*/
var TimeoutError;
/**
* Rejects a promise with a {@link TimeoutError} if it does not settle within
* the specified timeout.
*
* @param {Promise} promise The promise.
* @param {number} timeoutMillis Number of milliseconds to wait on settling.
* @returns {Promise} Either resolves/rejects with `promise`, or rejects with
* `TimeoutError`, whichever settles first.
*/
var timeout = module.exports.timeout = function(promise, timeoutMillis) {
var error = new TimeoutError(),
timeout;
return Promise.race([
promise,
new Promise(function(resolve, reject) {
timeout = setTimeout(function() {
reject(error);
}, timeoutMillis);
}),
]).then(function(v) {
clearTimeout(timeout);
return v;
}, function(err) {
clearTimeout(timeout);
throw err;
});
};
/**
* Exception indicating that the timeout expired.
*/
TimeoutError = module.exports.TimeoutError = function() {
Error.call(this)
this.stack = Error().stack
this.message = 'Timeout';
};
TimeoutError.prototype = Object.create(Error.prototype);
TimeoutError.prototype.name = "TimeoutError";