1

Let's say I have 4 API calls that might call more APIs. Then, I go into main() {} and I write

api1();
api2();
api3();

While the body of each is:

{
 await getData();
 getMoreData();
 getMoreData();
}

Note that I don't have awaits in main and also in my function body.

My question is, will that mean that the first 3 functions will be executed at the same time and then when the others run, they will also be at the same time?

Miro
  • 364
  • 1
  • 12

2 Answers2

2

My question is, will that mean that the first 3 functions will be executed at the same time and then when the others run, they will also be at the same time?

It depends on exactly what you mean by "at the same time". When you do:

void main() {
  api1();
  api2();
  api3();
}

and if api1, api2, and api3 are all asynchronous, then they will run concurrently. They might or might not run in parallel.

Note that if api1, api2, and api3 each returns a Future, then none of those Futures are awaited and you will not be notified when any or all of them complete. If you want to be notified when all of them complete, then use Future.wait. If you don't care and want to treat them as fire-and-forget operations, then Future.wait isn't necessary.

If you also want to silently swallow errors from api1, api2, and api3, then you should use the Future.ignore extension method:

void main() {
  api1().ignore();
  api2().ignore();
  api3().ignore();
}
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
1

Yes you can do so by the use of Future.wait and Future.any functions.

Future.wait() completes when all the futures in the list complete successfully. If any future in the list fails with an error, the returned future will also fail with that error.

Future.any() completes as soon as any of the futures in the list completes successfully. The returned future will complete with the value of the first future in the list that completes successfully.

Here is a sample code that may work for you:

    Future<void> runAsyncFunctions() async {
    // Create a list of futures
    List<Future<void>> futures = [
    asyncFunction1(),
    asyncFunction2(),
    asyncFunction3(),
    ];
  
    // Wait for all the futures to complete
    await Future.wait(futures);
  
   // All the futures have completed at this point
   }
Naqeeb Abid
  • 116
  • 5
  • Hi, I don't want to wait for the functions to complete but rather have them run at the same time. What will the result of my code be? I also don't want any risk of my Future failing if any of them fail. I will run multiple APIs at the same time which will, for example, add values to a list. – Miro Apr 28 '23 at 11:13
  • In the example I provided, the async functions will run concurrently because their futures are added to the futures list without awaiting their completion. Therefore, in the example I provided, asyncFunction1(), asyncFunction2(), and asyncFunction3() will start running as soon as they are called, without waiting for any other function to complete. The order in which they complete may not be predictable. – Naqeeb Abid Apr 28 '23 at 11:49
  • So is it correct that as long as I don't call `Future.wait(futures);`, futures won't return an error? Also, can you explain if this is any different than writing: `asyncFunction1();asyncFunction2();asyncFunction3();` – Miro Apr 28 '23 at 14:13