Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter multiple async methods for parrallel execution

I'm still struggeling with the async/await pattern so I'm here to ask you some precisions.

I saw this page explaining the async/await pattern pretty well. I'm posting here the example that bother me :

import 'dart:async';  Future<String> firstAsync() async {   await Future<String>.delayed(const Duration(seconds: 2));   return "First!"; }  Future<String> secondAsync() async {   await Future<String>.delayed(const Duration(seconds: 2));   return "Second!"; }  Future<String> thirdAsync() async {   await Future<String>.delayed(const Duration(seconds: 2));   return "Third!"; }  void main() async {   var f = await firstAsync();   print(f);   var s = await secondAsync();   print(s);   var t = await thirdAsync();   print(t);   print('done'); } 

In this example, each async method is called one after another, so the execution time for the main function is 6 seconds (3 x 2 seconds). However, I don't understand what's the point of asynchronous function if they are executed one after another.

Are async functions not supposed to execute in the background ? Is it not the point of multiple async functions to fastens the process with parrallel execution ?

I think I'm missing something about asynchronous functions and async/await pattern in flutter so if you could explain me that, it would be very appreciated.

Best

like image 966
Maxouille Avatar asked Apr 03 '19 19:04

Maxouille


People also ask

Do async functions run in parallel?

Asynchronous operations in parallelThe method async. parallel() is used to run multiple asynchronous operations in parallel. The first argument to async. parallel() is a collection of the asynchronous functions to run (an array, object or other iterable).

Can one async have two awaits?

all the two await calls can be started before either one is resolved. The answer is misleading. The jsbin code appears to be executing promises in parallel, but they are not.

Can we use async without await in flutter?

Sure, just omit await . This way callee() is called immediately and when an async operation is called the call will be scheduled in the event queue for later execution and caller() is continued immediately afterwards.

What is async and async * In flutter?

Async and Await keywords are used to provide a declarative way to define the asynchronous function and use their results. The async keyword is used when we want to declare a function as asynchronous and the await keyword is used only on asynchronous functions.


1 Answers

Waiting on multiple Futures to complete using Future.wait() If the order of execution of the functions is not important, you can use Future.wait().

The functions get triggered in quick succession; when all of them complete with a value, Future.wait() returns a new Future. This Future completes with a list containing the values produced by each function.

Future     .wait([firstAsync(), secondAsync(), thirdAsyncC()])     .then((List responses) => chooseBestResponse(responses))     .catchError((e) => handleError(e)); 

or with async/await

try {     List responses = await Future.wait([firstAsync(), secondAsync(), thirdAsyncC()]); } catch (e) {     handleError(e) } 

If any of the invoked functions completes with an error, the Future returned by Future.wait() also completes with an error. Use catchError() to handle the error.

Resource:https://v1-dartlang-org.firebaseapp.com/tutorials/language/futures#waiting-on-multiple-futures-to-complete-using-futurewait

like image 103
Дмитрий Платонов Avatar answered Sep 22 '22 03:09

Дмитрий Платонов