Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kkristof200/dart-future-pool


https://github.com/kkristof200/dart-future-pool

Last synced: 10 days ago
JSON representation

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}');
}
}
}
```