12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- // 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";
|