when 0.2.0

  • README.md
  • Installing
  • Versions
  • 1

when pub package Build Status Coverage Status

It's often useful to provide sync (convenient) and async (concurrent) versions of the same API. dart:io does this with many APIs including Process.run and Process.runSync. Since the sync and async versions do the same thing, much of the logic is the same, with just a few small bits differing in their sync vs. async implementation.

The when function allows for registering onSuccess, onError, and onComplete callbacks on another callback which represents that sync/async dependent part of the API. If the callback is sync (returns a non-Future or throws), then the other callbacks are invoked synchronously, otherwise the other callbacks are registered on the returned Future.

For example, here's how it can be used to implement sync and async APIs for reading a JSON data structure from the file system with file absence handling:

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:when/when.dart';

/// Reads and decodes JSON from [path] asynchronously.
/// If [path] does not exist, returns the result of calling [onAbsent].
Future readJsonFile(String path, {onAbsent()}) => _readJsonFile(
    path, onAbsent, (file) => file.exists(), (file) => file.readAsString());

/// Reads and decodes JSON from [path] synchronously.
/// If [path] does not exist, returns the result of calling [onAbsent].
readJsonFileSync(String path, {onAbsent()}) => _readJsonFile(
    path, onAbsent, (file) => file.existsSync(),
    (file) => file.readAsStringSync());

_readJsonFile(String path, onAbsent(), exists(File file), read(File file)) {
  var file = new File(path);
  return when(
      () => exists(file),
      onSuccess: (doesExist) => doesExist ?
          when(() => read(file), onSuccess: JSON.decode) :

main() {
  var syncJson = readJsonFileSync('foo.json', onAbsent: () => {'foo': 'bar'});
  print('Sync json: $syncJson');
  readJsonFile('foo.json', onAbsent: () => {'foo': 'bar'}).then((asyncJson) {
    print('Async json: $asyncJson');


  • Make onSuccess optional.


  • Initial version.

Use this package as a library

1. Depend on it

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

  when: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

import 'package:when/when.dart';
Version Uploaded Documentation Archive
0.2.0 Jan 13, 2015 Go to the documentation of when 0.2.0 Download when 0.2.0 archive
0.1.0 Jan 8, 2015 Go to the documentation of when 0.1.0 Download when 0.1.0 archive
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.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Health suggestions

Format lib/when.dart.

Run dartfmt to format lib/when.dart.

Maintenance issues and suggestions

Add SDK constraint in pubspec.yaml. (-50 points)

For information about setting SDK constraint, please see https://www.dartlang.org/tools/pub/pubspec#sdk-constraints.

Package is too old. (-100 points)

The package was released more than two years ago.

Homepage URL does not exists. (-20 points)

We were unable to access https://github.com/seaneagan/when.dart at the time of the analysis.

Maintain an example.

None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use when.dart. Packages with multiple examples should use example/readme.md.


Package Constraint Resolved Available
Dev dependencies
unittest >=0.11.4 <0.12.0