Skip to content

Commit

Permalink
Add custom error message to Q.timeout method
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrenon authored and domenic committed Apr 15, 2013
1 parent c862b7f commit 27e3548
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1334,14 +1334,15 @@ function done(promise, fulfilled, rejected, progress) {
* some milliseconds time out.
* @param {Any*} promise
* @param {Number} milliseconds timeout
* @param {String} custom error message (optional)
* @returns a promise for the resolution of the given promise if it is
* fulfilled before the timeout, otherwise rejected.
*/
Q.timeout = timeout;
function timeout(promise, ms) {
function timeout(promise, ms, msg) {
var deferred = defer();
var timeoutId = setTimeout(function () {
deferred.reject(new Error("Timed out after " + ms + " ms"));
deferred.reject(new Error(msg || "Timed out after " + ms + " ms"));
}, ms);

when(promise, function (value) {
Expand Down
16 changes: 16 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,22 @@ describe("timeout", function () {

return promise;
});

it("should reject with a custom timeout error if the promise is too slow and msg was provided", function () {
var goodError = new Error("haha!");
return Q.delay(100)
.timeout(10, "custom")
.then(
function () {
expect(true).toBe(false);
},
function (error) {
expect(/custom/i.test(error.message)).toBe(true);
}
);
});


});

describe("delay", function () {
Expand Down

0 comments on commit 27e3548

Please sign in to comment.