-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 'timeout' - Promise extension | ||
// | ||
// promise.timeout(ms) | ||
// | ||
// Resolves with resolution value of context promise assuming it settles before timeout time passes | ||
// Otherwise resolves with timeout rejection | ||
|
||
"use strict"; | ||
|
||
var customError = require("es5-ext/error/custom") | ||
, isValue = require("es5-ext/object/is-value") | ||
, nextTick = require("next-tick") | ||
, ensureTimeout = require("timers-ext/valid-timeout") | ||
, deferred = require("../../deferred"); | ||
|
||
deferred.extend( | ||
"timeout", | ||
function (timeout) { | ||
var def; | ||
var callback = function () { | ||
if (this.resolved) return; | ||
def.reject(customError("Operation timeout", "DEFERRED_TIMEOUT")); | ||
}.bind(this); | ||
if (isValue(timeout)) setTimeout(callback, ensureTimeout(timeout)); | ||
else nextTick(callback); | ||
if (!this.pending) this.pending = []; | ||
def = deferred(); | ||
this.pending.push("timeout", [def.promise, def.resolve]); | ||
return def.promise; | ||
}, | ||
function (promise, resolve) { | ||
if (!promise.resolved) resolve(this); | ||
}, | ||
function (timeout) { | ||
if (isValue(timeout)) ensureTimeout(timeout); | ||
return this; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
|
||
var Deferred = require("../../../deferred"); | ||
|
||
module.exports = function (t, a, d) { | ||
var x = {}, deferred = new Deferred(), promise = deferred.promise.timeout(10); | ||
deferred.resolve(x); | ||
setTimeout(function () { | ||
a(promise.value, x); | ||
deferred = new Deferred(); | ||
promise = deferred.promise.timeout(10); | ||
setTimeout(function () { | ||
deferred.resolve(); | ||
a(promise.failed, true); | ||
a(promise.value.code, "DEFERRED_TIMEOUT"); | ||
d(); | ||
}, 20); | ||
}, 20); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters