Skip to content

Commit

Permalink
Invoke clearTimeout if approriate inside promise.timeout function
Browse files Browse the repository at this point in the history
  • Loading branch information
bryaningl3 committed Aug 13, 2023
1 parent bb46d58 commit 0cafd1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .releases/4.29.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug Fixes**

* Fixed issue with `promise.timeout` function.
33 changes: 26 additions & 7 deletions lang/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,32 @@ module.exports = (() => {
return Promise.reject('Unable to configure promise timeout, the "milliseconds" argument must be positive');
}

return Promise.race([
promise, this.build((resolveCallback, rejectCallback) => {
setTimeout(() => {
rejectCallback(description || `Promise timed out after ${milliseconds} milliseconds`);
}, milliseconds);
})
]);
let timeoutToken = null;

const timeoutPromise = this.build((resolveCallback, rejectCallback) => {
timeoutToken = setTimeout(() => {
rejectCallback(description || `Promise timed out after ${milliseconds} milliseconds`);
}, milliseconds);
});

const userPromise = Promise.resolve()
.then(() => {
return promise;
}).then((result) => {
if (timeoutToken !== null) {
clearTimeout(timeoutToken);
}

return result;
}).catch((e) => {
if (timeoutToken !== null) {
clearTimeout(timeoutToken);
}

return Promise.reject(e);
});

return Promise.race([ userPromise, timeoutPromise ]);
});
},

Expand Down

0 comments on commit 0cafd1d

Please sign in to comment.