future_pool 0.0.2 copy "future_pool: ^0.0.2" to clipboard
future_pool: ^0.0.2 copied to clipboard

Execute multiple async functions in parallel with a pooling mechanism

future_pool #

Description #

Execute multiple async functions in parallel with a pooling mechanism.

Installing #

dart pub add future_pool

or

flutter pub add future_pool

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}');
    }
  }
}
0
likes
130
pub points
0%
popularity

Publisher

unverified uploader

Execute multiple async functions in parallel with a pooling mechanism

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

synchronized

More

Packages that depend on future_pool