Skip to content

Commit c95ca24

Browse files
authored
fix - usetimeout (#10893)
1 parent ba3f12f commit c95ca24

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
export const withTimeout = (millis: number, promise: any, id: string = "") => {
2-
const timeout = new Promise((resolve, reject) =>
3-
setTimeout(() => {
2+
let timeoutId: NodeJS.Timeout;
3+
4+
const timeout = new Promise((_, reject) => {
5+
timeoutId = setTimeout(() => {
46
reject(`${id} timed out after ${millis / 1000} s.`);
5-
resolve;
6-
}, millis)
7-
);
8-
return Promise.race([promise, timeout]);
9-
};
7+
}, millis);
8+
});
9+
10+
return Promise.race([promise, timeout]).finally(() => {
11+
clearTimeout(timeoutId);
12+
});
13+
};

0 commit comments

Comments
 (0)