Concurrent Asynchronous Operations in Dart: A Guide to Future.wait

Concurrent Asynchronous Operations in Dart: A Guide to Future.wait


Introduction:

Dart has provided a method “Future.wait” that provides an efficient way to handle multiple asynchronous operations concurrently. This is useful when you have several async functions that need to be executed before performing any operation. Please note that these async functions should be independent of each other. 

Let’s consider you have to fetch data from 10 different APIs that are not dependent on each other. For the demo, I am using JSON Placeholder API to fetch To-dos. 

Firstly, implement the API, it’s pretty simple. 

Article content
Implementation of async operation: API call

On the second step, let’s create a list of API calls

Article content
List of async operation

Then, pass the list of API calls that we have created to the Future.wait() method. And then await for the result.

Article content
Implementation of Future.wait() method

Done, you have successfully implemented Future.wait() and handled multiple asynchronous operations efficiently. 

Article content
Wait a minute, meme

You might be thinking, aren’t we missing anything? Yes, we are. 

Error Handling.

On our async function we are throwing an exception, to handle that we need to surround Future.wait() with try catch block like this.

Article content
Handling error

If you want to do some cleanup, then you can create a function where you handle cleanup. Clean up function is used for closing DB, file or any connection and so on. 

Note: cleanup function will be only executed if any async function completes with an error. 

Here’s the complete code:

Article content
Complete code


In this example:

  • fetchTodoData simulates an asynchronous operation of fetching data from the JSON Placeholder API.
  • If the HTTP request is successful (status code 200), the response is parsed and returned as a list of to-dos.
  • If the HTTP request fails or if there is a network-related error, an exception is thrown.
  • The main function catches errors, prints an error message, and invokes the cleanUpResources function.
  • The cleanUpResources function is a placeholder with a print statement. In a real-world scenario, this function should perform actual cleanup tasks, such as closing connections or releasing resources.



Comparing the execution time of Future.wait() with multiple await statements.

Fetching 10 APIs using Future.wait() method take only 31ms. 

Article content
Execution time of Future.wait()

Using await

Article content
await implementation for multiple async

Output: 

Article content
Execution time of await

You can see that, Future.wait() does the same task a lot faster than using individual await on all each async function. 

Article content
Comparison of execution time
Less is better.


Conclusion:

In conclusion, Future.wait() method is a way more efficient for handling multiple async operations which are independent of each other. By using Future.wait, developers can streamline their code and enhance overall performance.


I hope you found this guide on Dart's Future.wait helpful! Your feedback matters. If you have questions, suggestions, or specific topics you'd like more coverage on, please leave a comment. Your insights help improve future content to better suit your needs. Thanks for reading! 🚀


Link:

  1. https://api.flutter.dev/flutter/dart-async/Future/wait.html
  2. https://gist.github.com/sawin0/1f6524be429ac20fc94dc1b1f4771791

To view or add a comment, sign in

More articles by Sabin Ranabhat

Explore content categories