Retry
When a function may succeed after some attempts, you might need a retry logic - keep trying the function until it succeeds, or after a certain number of attempts. You want some kind of waiting period between each attempt, eg to wait for a network connection.
This can be achieved using Flow.retryFunction .
In the example, we will try up to five times to run the async function doSomething
, starting with 1 second delay if it fails. This time gets longer and longer with each attempt.
import { retry } from "https://unpkg.com/ixfx/dist/flow.js"
const doSomething = () => { // If this function throws an error or returns undefined, // it's assumed to have failed
// for it to succeed it has to return a value}
const result = await retryFunction(doSomething, { limitAttempts: 5, startAt: 1000 });if (result.success) { // Yay result.value will contain the result of `doSomething`} else { // result.message tells why it failed}
See also:
- Flow.waitFor - call a function and be notified if it doesn’t finish within a certain time
Advanced
Options :
taskValueFallback
: option can provide a value if the task ultimately failsabort
: an abort signal to cancel waitingpredelayMs
: time to wait before first attemptlimitAttempts
: total number of times to retrylimitValue
: limit for how many milliseconds to waitstartAt
: starting milliseconds