Plato on Github
Report Home
node_modules/when/lib/decorators/timed.js
Maintainability
80.73
Lines of code
64
Difficulty
15.72
Estimated Errors
0.36
Function weight
By Complexity
By SLOC
/** @license MIT License (c) copyright 2010-2014 original author or authors */ /** @author Brian Cavalier */ /** @author John Hann */ (function(define) { 'use strict'; define(function(require) { var timer = require('../timer'); var TimeoutError = require('../TimeoutError'); return function timed(Promise) { /** * Return a new promise whose fulfillment value is revealed only * after ms milliseconds * @param {number} ms milliseconds * @returns {Promise} */ Promise.prototype.delay = function(ms) { var p = this._beget(); var h = p._handler; this._handler.map(function delay(x) { timer.set(function() { h.resolve(x); }, ms); }, h); return p; }; /** * Return a new promise that rejects after ms milliseconds unless * this promise fulfills earlier, in which case the returned promise * fulfills with the same value. * @param {number} ms milliseconds * @param {Error|*=} reason optional rejection reason to use, defaults * to an Error if not provided * @returns {Promise} */ Promise.prototype.timeout = function(ms, reason) { var hasReason = arguments.length > 1; var p = this._beget(); var h = p._handler; var t = timer.set(onTimeout, ms); this._handler.chain(h, function onFulfill(x) { timer.clear(t); this.resolve(x); // this = p._handler }, function onReject(x) { timer.clear(t); this.reject(x); // this = p._handler }, h.notify); return p; function onTimeout() { h.reject(hasReason ? reason : new TimeoutError('timed out after ' + ms + 'ms')); } }; return Promise; }; }); }(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));