Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit ed0c948

Browse files
authored
Merge pull request #109 from airbnb/cleartimeout-in-raceto
Clear timeout in raceTo
2 parents 82d66f3 + fdabc6e commit ed0c948

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/utils/lifecycle.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,22 @@ export function hasMethod(name) {
2424
* @returns {Promise}
2525
*/
2626
export function raceTo(promise, ms, msg) {
27+
let timeout;
28+
2729
return Promise.race([
2830
promise,
29-
new Promise(resolve => setTimeout(() => resolve(PROMISE_TIMEOUT), ms)),
31+
new Promise((resolve) => {
32+
timeout = setTimeout(() => resolve(PROMISE_TIMEOUT), ms);
33+
}),
3034
]).then((res) => {
3135
if (res === PROMISE_TIMEOUT) logger.info(msg, { timeout: ms });
36+
if (timeout) clearTimeout(timeout);
3237

3338
return res;
39+
}).catch((err) => {
40+
if (timeout) clearTimeout(timeout);
41+
42+
return Promise.reject(err);
3443
});
3544
}
3645

0 commit comments

Comments
 (0)