Swiftybase
Function StackCustom Function

Await Function

Wait for single or multiple asynchronous functions or operations to complete.

Config

  • Async Value: The input can be either:

    • A single Async[T] value
    • A list of async values like List[Async[String]] or a different type of async as List[Async[Any]]
  • Error Strategy: Define how you want to handle errors:

    • Fail Fast: Returns the result wrapped in a Result containing the first operation that resolved with an error. If all operations succeed, it returns a Result[T] where T is either a single value or list of values based on the provided async input.
    • Collect All: Waits until all operations complete (success or failure) and returns a List[Result[T]] with each operation containing either a value or error. For a single value input, it will just return Result[T].
  • Force Unwrap: Unwraps the value from the result if no error, or panics if an error is present.


Example 1: Running Operations Concurrently

In this example, we define a custom function that waits 2 seconds and returns a random ID. We call this function multiple times concurrently to demonstrate that both operations complete in approximately 2 seconds total (not 4 seconds), as they run in parallel.

Define a Custom Function

First, create a custom function named GetRandomId that returns one output value as String. Inside the function, use the Create Random String function and return the generated value.

Custom function definition

Call Function Asynchronously

Call the function with the Run Asynchronously option enabled to run in a new Goroutine. Store the async result in a variable named asyncRes1.

First async function call

Add Another Async Call

Call the same function again and store the result as asyncRes2.

Create a List of Async Values

Create a list of type List[Async[String]] containing both async values. This will give you a tasks list variable holding both async operations.

Creating a list of async values

Await the Async Operations

Provide the tasks list to the Await Function to resolve all async operations.

Based on the input type and error strategy chosen:

  • For a single value: returns Res[T]
  • For multiple values with Fail Fast: returns Res[List[T]] with values in the same order as the defined async inputs
  • For multiple values with Collect All: returns List[Res[T]] with results in the same order as the defined async inputs

Await Function configuration

Testing

When testing the function, you'll see that both operations complete in about 2 seconds since they run concurrently.

Test results showing concurrent execution

You can record the start and end times to verify the total execution time:

Timing verification

Error Handling

With Fail Fast strategy, if one operation fails, it returns an error immediately:

Fail Fast error handling

With Collect All strategy, it returns a list with each value wrapped in its own Result, containing either a value or error:

Collect All error handling


Example 2: Running Operations Concurrently on Shared Data

In this example, we define a custom function that concurrently calls an external API and adds the response back to a SyncMap without returning data.

Define a SyncMap

Create a new SyncMap by using Make Map and set the type SyncMap[String, Any] to make a safe map that can be used in multiple threads (Goroutines).

Define a Custom Function

Create a custom function named GetExternalData that takes one parameter named url and returns no output value.

Inside the function, use the External API Request function to call the API for the passed URL and configure to get the result as Map[String, Any] as the response value and force unwrap the result.

If getting data fails, it panics and the resolving value will fail as well.

We recommend to always handle errors and return data to the caller by defining functions with return values, but sometimes you don't need to return or need to add values to a shared variable that can be used by other async tasks to process. If that's not the case, returning data will be much easier to maintain.

Call Function Asynchronously

Call the function with the Run Asynchronously option enabled to run in a new Goroutine and provide the URL to get the data. In this example we use: https://jsonplaceholder.typicode.com/users/1

Store the async result in a variable named asyncRes1.

Define Variables Exposed to Async Scope

Click on the configure under the Shared Scope and add variables that can be used in multiple workers. In this example we add syncMapRes to the shared scope.

Add Another Async Call

Call the same function again and store the result as asyncRes2.

Ensure you provide a different URL so that we see the difference. For example, we used: https://jsonplaceholder.typicode.com/users/2

Create a List of Async Values

Create a list of type List[Async[String]] containing both async values. This will give you a tasks list variable holding both async operations.

Creating a list of async values

Await the Async Operations

Provide the tasks list to the Await Function to resolve all async operations.

Await Function configuration

Testing

When testing the function, you'll see that both operations simultaneously call the external API and when they get the result, they add it to the sync map with the key as URL and response as value.