Skip to content

Commit

Permalink
Prevent memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dfilatov authored and domenic committed Jun 25, 2012
1 parent 0428c15 commit d920b30
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit d920b30

Please sign in to comment.