Skip to content

Commit 27e3548

Browse files
jgrenondomenic
authored andcommitted
Add custom error message to Q.timeout method
1 parent c862b7f commit 27e3548

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

q.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,14 +1334,15 @@ function done(promise, fulfilled, rejected, progress) {
13341334
* some milliseconds time out.
13351335
* @param {Any*} promise
13361336
* @param {Number} milliseconds timeout
1337+
* @param {String} custom error message (optional)
13371338
* @returns a promise for the resolution of the given promise if it is
13381339
* fulfilled before the timeout, otherwise rejected.
13391340
*/
13401341
Q.timeout = timeout;
1341-
function timeout(promise, ms) {
1342+
function timeout(promise, ms, msg) {
13421343
var deferred = defer();
13431344
var timeoutId = setTimeout(function () {
1344-
deferred.reject(new Error("Timed out after " + ms + " ms"));
1345+
deferred.reject(new Error(msg || "Timed out after " + ms + " ms"));
13451346
}, ms);
13461347

13471348
when(promise, function (value) {

spec/q-spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,22 @@ describe("timeout", function () {
15831583

15841584
return promise;
15851585
});
1586+
1587+
it("should reject with a custom timeout error if the promise is too slow and msg was provided", function () {
1588+
var goodError = new Error("haha!");
1589+
return Q.delay(100)
1590+
.timeout(10, "custom")
1591+
.then(
1592+
function () {
1593+
expect(true).toBe(false);
1594+
},
1595+
function (error) {
1596+
expect(/custom/i.test(error.message)).toBe(true);
1597+
}
1598+
);
1599+
});
1600+
1601+
15861602
});
15871603

15881604
describe("delay", function () {

0 commit comments

Comments
 (0)