Skip to content

Commit

Permalink
feat: timeout promise extension
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Dec 13, 2017
1 parent 65df0e5 commit cbdc9fd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,32 @@ promise.spread(function (a, b) {
});
```
#### timeout
Resolve with rejection ("Operation timeout" error with `DEFERRED_TIMEOUT` code), if given promise
won't settle with resolved value within given _timeout_
```javascript
var deferred = new Deferred();

deferred.promise.timeout(30).done(function (value) {
console.log("Never called");
}, function () {
console.log("Rejected after 30 seconds");
});

deferred = new Deferred();
deferred.promise.timeout(30).done(function (value) {
console.log("Resolved with", value); // Resolved with true
}, function () {}
console.log("Never called");
});
setTimeout(function () {
deferred.resolve(true);
}, 10);

```
### Debugging
#### Monitoring unresolved promises
Expand Down Expand Up @@ -771,4 +797,4 @@ __Covered by over 300 unit tests__
## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgit.luolix.top%2Fmedikoo%2Fdeferred.svg?type=large)](https://app.fossa.io/projects/git%2Bgit.luolix.top%2Fmedikoo%2Fdeferred?ref=badge_large)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgit.luolix.top%2Fmedikoo%2Fdeferred.svg?type=large)](https://app.fossa.io/projects/git%2Bgit.luolix.top%2Fmedikoo%2Fdeferred?ref=badge_large)
38 changes: 38 additions & 0 deletions ext/promise/timeout.js
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;
}
);
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ require("./ext/promise/map");
require("./ext/promise/reduce");
require("./ext/promise/spread");
require("./ext/promise/some");
require("./ext/promise/timeout");
19 changes: 19 additions & 0 deletions test/ext/promise/timeout.js
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);
};
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ module.exports = {
"find",
"map",
"reduce",
"some"
"some",
"timeout"
]
),
"isPromise": function (t, a) {
Expand Down

0 comments on commit cbdc9fd

Please sign in to comment.