How to retry the async function with vRO?

Learn how to write a modern retry function for vRO

Hey there! Do you happen to know how we can retry an async function on vRO? Let's figure it out.

Problem

Let’s say we have some async function myFunction that we want to execute multiple times until we get the expected result.

public myFunction = async ( varA: string ) => {
  return await someAsyncFunction( varA )
}

Solution

To achieve this goal, we need a retry function. Let’s see how we can make one.

  1. The function takes the following parameters:
  2. func: An asynchronous function that will be retried.
  3. retries: The maximum number of retries (default is 5).
  4. interval: The interval (in milliseconds) between retries (default is 10,000 ms or 10 seconds).
  5. progressive: A boolean flag indicating whether to use progressive backoff for the interval (default is false).
  6. It tries to execute the provided function (func) using await.
  7. If an error occurs during execution, it checks if there are retries left:
  8. If retries are available, it logs the retry number, waits for the specified interval, and recursively calls itself with reduced retries and possibly an increased interval (if progressive is true).
  9. If no retries are left, it throws an error indicating that the maximum retries have been reached for the given function.
public async retryPromise<T>(
    func: () => Promise<T>,
    retries: number = 5,
    interval: number = 10000,
    progressive: boolean = false
  ): Promise<T> {
    try {
      return await func()
    } catch ( error ) {
      if ( retries ) {
        System.log( `Retry number ${retries}` )
        await new Promise( ( resolve ) => resolve( System.sleep( interval ) ) )
        return this.retryPromise( func, retries - 1, progressive ? interval * 2 : interval, progressive )
      } else throw new Error( `Max retries reached for function ${func.name}` )
    }
}

Implementation example

In that example, we're executing a function myFunction 20 times, with an interval of 60 seconds without progressive.

public async main () {
  return this.retryPromise( () => this.myFunction( varA ), 20, 60000, false )
}

Summary

Today we saw how we can improve the retry logic by using some modern language techniques.

Table of Contents
Great! Next, complete checkout for full access to CloudDepth.
Welcome back! You've successfully signed in.
You've successfully subscribed to CloudDepth.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.

This work by Leonid Belenkiy is licensed under Creative Commons Attribution 4.0 International