Opal-Estate-Pro/node_modules/timers-ext/promise_/timeout.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-09-13 06:27:52 +02:00
"use strict";
var customError = require("es5-ext/error/custom")
, isValue = require("es5-ext/object/is-value")
, ensurePromise = require("es5-ext/object/ensure-promise")
, nextTick = require("next-tick")
, ensureTimeout = require("../valid-timeout");
module.exports = function (/* timeout */) {
ensurePromise(this);
var timeout = arguments[0];
if (isValue(timeout)) timeout = ensureTimeout(timeout);
return new this.constructor(
function (resolve, reject) {
var isSettled = false, timeoutId;
var timeoutCallback = function () {
if (isSettled) return;
reject(
customError(
"Operation timeout (exceeded " +
(isValue(timeout) ? timeout + "ms" : "tick") +
")",
"PROMISE_TIMEOUT"
)
);
};
if (isValue(timeout)) timeoutId = setTimeout(timeoutCallback, timeout);
else nextTick(timeoutCallback);
this.then(
function (value) {
isSettled = true;
clearTimeout(timeoutId);
resolve(value);
},
function (reason) {
isSettled = true;
clearTimeout(timeoutId);
reject(reason);
}
);
}.bind(this)
);
};