f4 1.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

A Future with some added sugar. #

A set of Future helper functions (mostly) compatible with Node.js counterpart Promise libraries such as Bluebird. Supports concurrency & cancellation.

The library exposes static methods under a single class F4 (pronounced Future) to avoid accendential pollution of the global namespace, maybe a bit of an anti-pattern in Dart land, then again maybe some of helpers should have been natively supported by Dart itself.

Currently supports the following helper methods: all, map, chain, props, one, forEach, create, and reflect.

The AbortController class exposes means for cancellation of Futures and is compatible with its Fetch API counterpart.

PS: Not tested in production, written over a weekend for me to learn Dart, PRs are welcome.

Usage #

A simple usage example:

import 'package:f4/f4.dart';

main() async
  final r = await F4.map([1,2,3,4], (int i)=>Future.value(i), concurrency:2);
  print(r); //[1,2,3,4]

Complete usage example under /example.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

License #


1.0.4 #

1.0.3 #

  • Iterable should be Iterable Function in forEach constructor

1.0.2 #

  • dartfmt files

1.0.1 #

  • Minor README fix

1.0.0 #

  • Bump version, dartfmt, make pub.dev happy

0.0.1 #

  • Initial version, created by Stagehand


import 'dart:math';
import 'dart:async';
import 'package:f4/f4.dart';

Future sleep(int pnum, {int res, int ms = 500})
  pnum ??= 1;

  print('run #$pnum');

  return Future.delayed(Duration(milliseconds: Random().nextInt(ms) + ms))
        .then((_)=>pnum+(res ?? 0));

timeout(int s, Function fn)=>
  Timer(Duration(seconds:s), ()=> Function.apply(fn, null));

main() async
  List<Function> chain = [
    (int res)=>sleep(2, res:res),
    (int res)=>sleep(3, res:res),
    (int res)=>sleep(4, res:res)

  List<Function> list = [

  Map<String, Function> props = {
    'a': ()=>sleep(1),
    'b': ()=>sleep(2),
    'c': ()=>sleep(3),
    'd': ()=>sleep(4)

  print(await F4.forEach(list));

  print(await F4.chain(chain));

  print(await F4.all(list, concurrency:2));

  print(await F4.one(list[0]));

  print(await F4.props(props, concurrency:2));

  print(await F4.map([1,2,3,4], sleep, concurrency:2));

  final ctl1 = AbortController(onAbort:(e)=>print('onAbort: $e'));
  timeout(1, ()=>ctl1.abort('cancel'));
  final v = await F4.chain(chain, abortSignal:ctl1.signal);
  print('************* $v');

  final ctl2 = AbortController();
  timeout(1, ()=>ctl2.abort('cancel'));
  try {
    await F4.forEach(list, abortSignal:ctl2.signal);
  } catch(e) { print('catch: $e'); };

  print(await F4.create((resolve, reject)=>timeout(1, ()=>resolve(1))));

  try {
    await F4.create((resolve, reject)=>
      timeout(1, ()=>reject(RejectionError(message:'foo'))));
  } catch(e) { print(e.runtimeType);}

  print(await F4.reflect(Future.value(1)));
  print(await F4.reflect(Future.error('err')));

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  f4: ^1.0.4

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:f4/f4.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/abort.dart. (-0.50 points)

Analysis of lib/src/abort.dart reported 1 hint:

line 58 col 12: The value of the field '_onAbort' isn't used.

Format lib/src/reflect.dart.

Run dartfmt to format lib/src/reflect.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
meta ^1.1.7 1.1.8
Dev dependencies
pedantic ^1.7.0
test ^1.6.0