| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'package:expect/expect.dart'; |
| 7 |
| 8 typedef Future<Null> Task(); |
| 9 |
| 10 class ForeignFuture implements Future<Null> { |
| 11 ForeignFuture(List<Task> tasks) { |
| 12 tasks.forEach(_addTask); |
| 13 } |
| 14 |
| 15 Future<Null> _future; |
| 16 |
| 17 void _addTask(Task task) { |
| 18 _future = (_future == null) ? task() : _future.then((_) => task()); |
| 19 } |
| 20 |
| 21 Future<S> then<S>(FutureOr<S> onValue(Null _), {Function onError}) { |
| 22 assert(_future != null); |
| 23 return _future.then((_) { |
| 24 _future = null; |
| 25 return onValue(null); |
| 26 }, onError: (error, trace) { |
| 27 _future = null; |
| 28 if (onError != null) { |
| 29 onError(error, trace); |
| 30 } |
| 31 }); |
| 32 } |
| 33 |
| 34 Stream<Null> asStream() { |
| 35 return new Stream.fromFuture(this); |
| 36 } |
| 37 |
| 38 Future<Null> catchError(onError, {bool test(Object error)}) { |
| 39 print('Unimplemented catchError'); |
| 40 return null; |
| 41 } |
| 42 |
| 43 Future<Null> timeout(Duration timeLimit, {onTimeout()}) { |
| 44 print('Unimplemented timeout'); |
| 45 return null; |
| 46 } |
| 47 |
| 48 Future<Null> whenComplete(action()) { |
| 49 print('Unimplemented whenComplete'); |
| 50 return null; |
| 51 } |
| 52 } |
| 53 |
| 54 var r1; |
| 55 |
| 56 Future<Null> hello() async { |
| 57 r1 = 'hello'; |
| 58 throw new Exception('error'); |
| 59 } |
| 60 |
| 61 Future<String> world() async { |
| 62 try { |
| 63 await new ForeignFuture([hello]); |
| 64 } catch (e) {} |
| 65 return 'world'; |
| 66 } |
| 67 |
| 68 Future main() async { |
| 69 var r2 = await world(); |
| 70 Expect.equals('hello', r1); |
| 71 Expect.equals('world', r2); |
| 72 } |
| OLD | NEW |