Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(promise): promise.timeout method
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"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; | ||
this.then( | ||
function (value) { | ||
isSettled = true; | ||
resolve(value); | ||
}, | ||
function (reason) { | ||
isSettled = true; | ||
reject(reason); | ||
} | ||
); | ||
var timeoutCallback = function () { | ||
if (isSettled) return; | ||
reject(customError("Operation timeout", "PROMISE_TIMEOUT")); | ||
}; | ||
if (isValue(timeout)) setTimeout(timeoutCallback, timeout); | ||
else nextTick(timeoutCallback); | ||
}.bind(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"globals": { "Promise": true } | ||
} |
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,50 @@ | ||
"use strict"; | ||
|
||
var delay = require("../../delay"); | ||
|
||
module.exports = function (t, a) { | ||
if (typeof Promise !== "function") return null; | ||
return { | ||
Success: function (d) { | ||
var promise = t.call( | ||
new Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve("foo"); | ||
}, 20); | ||
}), | ||
40 | ||
); | ||
|
||
promise.then( | ||
// Delay to escape error swallowing | ||
delay(function (result) { | ||
a(result, "foo"); | ||
d(); | ||
}), | ||
delay(d) | ||
); | ||
}, | ||
Timeout: function (d) { | ||
var promise = t.call( | ||
new Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve("foo"); | ||
}, 40); | ||
}), | ||
20 | ||
); | ||
|
||
promise.then( | ||
// Delay to escape error swallowing | ||
delay(function () { | ||
a.never(); | ||
d(); | ||
}), | ||
delay(function (err) { | ||
a(err.code, "PROMISE_TIMEOUT"); | ||
d(); | ||
}) | ||
); | ||
} | ||
}; | ||
}; |