diff --git a/q.js b/q.js index 5cad0ce4..3a632483 100644 --- a/q.js +++ b/q.js @@ -1334,10 +1334,14 @@ function end(promise) { exports.timeout = timeout; function timeout(promise, ms) { var deferred = defer(); - when(promise, deferred.resolve, deferred.reject); - setTimeout(function () { - deferred.reject(new Error("Timed out after " + ms + "ms")); + var timeoutId = setTimeout(function () { + deferred.reject(new Error("Timed out after " + ms + " ms")); }, ms); + + when(promise, function (value) { + clearTimeout(timeoutId); + deferred.resolve(value); + }, deferred.reject); return deferred.promise; }