Skip to content

Commit

Permalink
fix(promise): clear timeout on promise resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 8, 2018
1 parent 43efbd1 commit 6301a6b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions promise_/timeout.js
Expand Up @@ -12,23 +12,25 @@ module.exports = function (/* timeout */) {
if (isValue(timeout)) timeout = ensureTimeout(timeout);
return new this.constructor(
function (resolve, reject) {
var isSettled = false;
var isSettled = false, timeoutId;
var timeoutCallback = function () {
if (isSettled) return;
reject(customError("Operation timeout", "PROMISE_TIMEOUT"));
};
if (isValue(timeout)) timeoutId = setTimeout(timeoutCallback, timeout);
else nextTick(timeoutCallback);
this.then(
function (value) {
isSettled = true;
clearTimeout(timeoutId);
resolve(value);
},
function (reason) {
isSettled = true;
clearTimeout(timeoutId);
reject(reason);
}
);
var timeoutCallback = function () {
if (isSettled) return;
reject(customError("Operation timeout", "PROMISE_TIMEOUT"));
};
if (isValue(timeout)) setTimeout(timeoutCallback, timeout);
else nextTick(timeoutCallback);
}.bind(this)
);
};

0 comments on commit 6301a6b

Please sign in to comment.