Skip to content

Commit 1cf7174

Browse files
authored
Merge pull request #141 from wheineman-sunrun/patch-1
Cancel timeout when it is no longer needed
2 parents ba17b92 + 941fd9c commit 1cf7174

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

sdk/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ export const retryFetch = (
3838

3939
const requestWrapper = (): Promise<Response> => {
4040
return new Promise((resolve, reject) => {
41-
if (timeout) setTimeout(() => reject('error: timeout'), timeout);
41+
let timeoutId: NodeJS.Timeout;
42+
if (timeout) {
43+
timeoutId = setTimeout(() => reject('error: timeout'), timeout);
44+
}
4245
return fetch(url, fetchOptions)
4346
.then(res => resolve(res))
4447
.catch(err => reject(err))
48+
.finally(() => {
49+
if (timeoutId) {
50+
clearTimeout(timeoutId);
51+
}
52+
})
4553
})
4654
}
4755

0 commit comments

Comments
 (0)