f4 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

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 #

MIT

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

example/f4_example.dart

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 = [
    ()=>sleep(1),
    (int res)=>sleep(2, res:res),
    (int res)=>sleep(3, res:res),
    (int res)=>sleep(4, res:res)
  ];

  List<Function> list = [
    ()=>sleep(1),
    ()=>sleep(2),
    ()=>sleep(3),
    ()=>sleep(4),
  ];

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

  print('---forEach---');
  print(await F4.forEach(list));

  print('\n---chain---');
  print(await F4.chain(chain));

  print('\n---all---');
  print(await F4.all(list, concurrency:2));

  print('\n---one---');
  print(await F4.one(list[0]));

  print('\n---props---');
  print(await F4.props(props, concurrency:2));

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

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

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

  print('\n---create---');
  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('\n---reflect---');
  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:


dependencies:
  f4: ^1.0.2

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Oct 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:f4/f4.dart.

Dependencies

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