Clone this repo:
  1. af3f0dd Merge pull request #13 from dart-lang/strong_mode by Phil Quitslund · 11 months ago master
  2. dda4427 Mark `when` strong-mode clean. by pq · 11 months ago strong_mode
  3. b768086 Merge pull request #12 from dart-lang/version_fix by Phil Quitslund · 2 years, 1 month ago 0.2.0+0 0.2.0+2
  4. e59714e Update pubspec.yaml by Phil Quitslund · 2 years, 1 month ago version_fix
  5. 5f7cd65 Roll coveralls back to latest. (Fix #10.) by Phil Quitslund · 2 years, 1 month ago

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