Sha256: 85a0a3fc3a1823fb6b1f5cd1727044934e805c33b157a11e1def59fca161029c
Contents?: true
Size: 1.7 KB
Versions: 19
Compression:
Stored size: 1.7 KB
Contents
"use strict"; module.exports = function(Promise, INTERNAL) { var util = require("./util.js"); var TimeoutError = Promise.TimeoutError; var afterTimeout = function (promise, message) { if (!promise.isPending()) return; var err; if(!util.isPrimitive(message) && (message instanceof Error)) { err = message; } else { if (typeof message !== "string") { message = "operation timed out"; } err = new TimeoutError(message); } util.markAsOriginatingFromRejection(err); promise._attachExtraTrace(err); promise._cancel(err); }; var afterValue = function(value) { return delay(+this).thenReturn(value); }; var delay = Promise.delay = function (value, ms) { if (ms === undefined) { ms = value; value = undefined; var ret = new Promise(INTERNAL); setTimeout(function() { ret._fulfill(); }, ms); return ret; } ms = +ms; return Promise.resolve(value)._then(afterValue, null, null, ms, undefined); }; Promise.prototype.delay = function (ms) { return delay(this, ms); }; function successClear(value) { var handle = this; if (handle instanceof Number) handle = +handle; clearTimeout(handle); return value; } function failureClear(reason) { var handle = this; if (handle instanceof Number) handle = +handle; clearTimeout(handle); throw reason; } Promise.prototype.timeout = function (ms, message) { ms = +ms; var ret = this.then().cancellable(); ret._cancellationParent = this; var handle = setTimeout(function timeoutTimeout() { afterTimeout(ret, message); }, ms); return ret._then(successClear, failureClear, undefined, handle, undefined); }; };
Version data entries
19 entries across 16 versions & 5 rubygems