#Try something N times

2 messages · Page 1 of 1 (latest)

fallen pawn
#

I have a Discord bot written in TypeScript.
It has a race condition that results in a run-time error.
The naive solution is to use a try / catch block to catch the error and retry the line of code until the error goes away.
This works. However, this also possibly introduces an infinite loop into the code, so I would to at most like to retry the line of code 5 times.
What is the most idiomatic way in TypeScript to retry something N times?
And can I compose a helper function to abstract this functionality away into something that can be cleanly used in application code?

mint field
#

probably just a for loop with a break on success or a while