Skip to content

Commit

Permalink
Better retry function
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Jun 12, 2023
1 parent 0fc18fa commit 4b0eb99
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/components/capabilities/implementation/fission-lobby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,23 @@ async function retry<T>(
action: () => Promise<T>,
options: { tries: number; timeout: number; timeoutMessage: string }
): Promise<T> {
return await Promise.race([
action(),
new Promise<T>((resolve, reject) => {
if (options.tries > 0) return setTimeout(
() => retry(action, { ...options, tries: options.tries - 1 }).then(resolve, reject),
options.timeout
)

return setTimeout(
() => reject(new Error(options.timeoutMessage)),
options.timeout
)
})
])
return new Promise((resolve, reject) => {
if (options.tries > 0) {
const unoMas = () => {
retry(action, { ...options, tries: options.tries - 1 })
}

const timeoutId = setTimeout(unoMas, options.timeout)

action()
.then(resolve, unoMas)
.finally(() => clearTimeout(timeoutId))

} else {
reject(new Error(options.timeoutMessage))

}
})
}


Expand Down

0 comments on commit 4b0eb99

Please sign in to comment.