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

Side by Side Diff: packages/when/example/read_json_file.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 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 unified diff | Download patch
« no previous file with comments | « packages/when/example/foo.json ('k') | packages/when/lib/when.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1
2 library when.example.read_json_file;
3
4 import 'dart:async';
5 import 'dart:convert';
6 import 'dart:io';
7
8 import 'package:when/when.dart';
9
10 /// Reads and decodes JSON from [path] asynchronously.
11 ///
12 /// If [path] does not exist, returns the result of calling [onAbsent].
13 Future readJsonFile(String path, {onAbsent()}) => _readJsonFile(
14 path, onAbsent, (file) => file.exists(), (file) => file.readAsString());
15
16 /// Reads and decodes JSON from [path] synchronously.
17 ///
18 /// If [path] does not exist, returns the result of calling [onAbsent].
19 readJsonFileSync(String path, {onAbsent()}) => _readJsonFile(
20 path, onAbsent, (file) => file.existsSync(),
21 (file) => file.readAsStringSync());
22
23 _readJsonFile(String path, onAbsent(), exists(File file), read(File file)) {
24 var file = new File(path);
25 return when(
26 () => exists(file),
27 onSuccess: (doesExist) => doesExist ?
28 when(() => read(file), onSuccess: JSON.decode) :
29 onAbsent());
30 }
31
32 main() {
33 var syncJson = readJsonFileSync('foo.json', onAbsent: () => {'foo': 'bar'});
34 print('Sync json: $syncJson');
35 readJsonFile('foo.json', onAbsent: () => {'foo': 'bar'}).then((asyncJson) {
36 print('Async json: $asyncJson');
37 });
38 }
OLDNEW
« no previous file with comments | « packages/when/example/foo.json ('k') | packages/when/lib/when.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698