Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kkristof200/dart-future-pool
https://github.com/kkristof200/dart-future-pool
Last synced: 10 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kkristof200/dart-future-pool
- Owner: kkristof200
- License: mit
- Created: 2024-07-02T15:14:12.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-07-03T16:49:12.000Z (5 months ago)
- Last Synced: 2024-07-04T06:21:45.136Z (4 months ago)
- Language: Dart
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
### future_pool
## Description
Execute multiple async functions in parallel with a pooling mechanism.
## Installing
```bash
dart pub add future_pool
```or
```bash
flutter pub add future_pool
```## Features
- Set maximum concurrency
- Optional callback for every successful/failed/all future results## Usage
```dart
import 'dart:math';
import 'package:future_pool/future_pool.dart';Future 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 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}');
}
}
}
```