How to cancel Future.wait (in dart)?

90 Views Asked by At

I have two Future functions which I run asynchronously:

List<Future<bool>> jobs = [];
jobs.add(asyncFunction1());
jobs.add(asyncFunction2());
jobsResults = await Future.wait(jobs);

If asyncFunction1 returns false - I want to finish current procedure (for not to wait asyncFunction2). How to do it?

1

There are 1 best solutions below

2
Lee Clayberg On

There isn't a standard way of doing this but a "hacky" way would be to have the functions throw errors if false. You could then utilize the eagerError=True (parameter for Future.wait). That will return immediately on the first error received.

Docs: https://api.dart.dev/stable/3.3.1/dart-async/Future/wait.html