extension FutureIterable<T>.wait
does not work properly
from the documentation:
/// Similar to [Future.wait], but reports errors using a
/// [ParallelWaitError], which allows the caller to
/// handle errors and dispose successful results if necessary.
The following code:
Future<int> main() async {
await process([1, 2, 3, 4, 5]);
return 0;
}
Future<void> process(Iterable<int> messages) async {
print("len: ${messages.length}");
Iterable<Future<int>> futures = messages.map((message) {
return fibo(message);
});
assert(messages.length == futures.length);
try {
print("before big wait");
await futures.wait;
print("after big wait");
} catch (e) {
print(e.runtimeType);
print("$e from the end");
}
print("stop!");
}
Future<int> fibo(int message) {
throw "Test error handling";
}
output:
flutter: len: 5
flutter: before big wait
flutter: String
flutter: Test error handling from the end
flutter: stop!
I don't see any sign of ParallelWaitError
, as the documentation mentions. And the future returns an error at the first exception.
What do I miss?
Instead of using throw
, try using Future.error
to return an error from your function:
Future<int> fibo(int message) {
return Future.error("Test error handling");
}
The result will show the ParallelWaitError
as you expect:
len: 5
before big wait
ParallelWaitError<List<int?>, List<AsyncError?>>
ParallelWaitError(5 errors): Test error handling from the end
stop!