-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cannot reset data while starting model
- Loading branch information
1 parent
0d31f9e
commit 637c0d8
Showing
3 changed files
with
49 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export function PromiseRetry<T>( | ||
promise: Promise<T>, | ||
times: number = 5, | ||
delay: number = 500 | ||
): Promise<T> { | ||
return new Promise<T>((resolve, reject) => { | ||
const retry = async () => { | ||
let lastError: Error | undefined | ||
for (let i = 0; i < times; i++) { | ||
try { | ||
const res = await promise | ||
resolve(res) | ||
return | ||
} catch (err) { | ||
lastError = err as Error | ||
console.warn(err) | ||
} | ||
await new Promise((resolve) => setTimeout(resolve, delay)) // 300 milliseconds | ||
} | ||
reject(lastError) | ||
} | ||
retry() | ||
}) | ||
} |