Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Unified Diff: packages/when/README.md

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « packages/when/LICENSE ('k') | packages/when/drone.sh » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/when/README.md
diff --git a/packages/when/README.md b/packages/when/README.md
new file mode 100755
index 0000000000000000000000000000000000000000..e3897a72e17f13f04deab81f0c7aa6073818f8da
--- /dev/null
+++ b/packages/when/README.md
@@ -0,0 +1,58 @@
+when [![pub package](http://img.shields.io/pub/v/when.svg)](https://pub.dartlang.org/packages/when) [![Build Status](https://drone.io/github.com/seaneagan/when.dart/status.png)](https://drone.io/github.com/seaneagan/when.dart/latest) [![Coverage Status](https://img.shields.io/coveralls/seaneagan/when.dart.svg)](https://coveralls.io/r/seaneagan/when.dart?branch=master)
+====
+
+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:
+
+```dart
+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) :
+ onAbsent());
+}
+
+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');
+ });
+}
+```
+
+[Process.run]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:io.Process#id_run
+[Process.runSync]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:io.Process#id_runSync
« no previous file with comments | « packages/when/LICENSE ('k') | packages/when/drone.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698