Last updated:
0 purchases
future pool
future_pool #
Description #
Execute multiple async functions in parallel with a pooling mechanism.
Installing #
dart pub add future_pool
copied to clipboard
or
flutter pub add future_pool
copied to clipboard
Features #
Set maximum concurrency
Optional callback for every successful/failed/all future results
Usage #
import 'dart:math';
import 'package:future_pool/future_pool.dart';
Future<int> future() async {
int seconds = Random().nextInt(5) + 1;
await Future.delayed(Duration(seconds: seconds));
return seconds;
}
void main() async {
final results = await FuturePool.allSettled(
List<Future<int> Function()>.filled(10, () => future()),
maxConcurrency: 3,
// onFutureCompleted: (result, index) {
// if (result.status == SettledStatus.fulfilled) {
// print('Future $index fulfilled with value: ${result.value}');
// } else {
// print('Future $index rejected with reason: ${result.reason}');
// }
// },
onFutureSuccess: (result, index) {
print('Future $index fulfilled with value: ${result.value}');
},
onFutureFail: (result, index) {
print('Future $index rejected with reason: ${result.reason}');
}
);
print('Finished all results $results');
for (var result in results) {
if (result.status == SettledStatus.fulfilled) {
print('Fulfilled: ${result.value}');
} else {
print('Rejected: ${result.reason}');
}
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.