Index: sdk/lib/async/future_impl.dart |
diff --git a/sdk/lib/async/future_impl.dart b/sdk/lib/async/future_impl.dart |
index 90e9df2b5033586837a245375526a067e24a7805..78bc979a8a831303a647b786bbc5010bfadb7897 100644 |
--- a/sdk/lib/async/future_impl.dart |
+++ b/sdk/lib/async/future_impl.dart |
@@ -537,4 +537,34 @@ class _Future<T> implements Future<T> { |
source = listener; |
} |
} |
+ |
+ Future timeout(Duration timeLimit, onTimeout()) { |
+ if (_isComplete) return new _Future.immediate(this); |
+ Zone zone = Zone.current; |
+ onTimeout = zone.registerCallback(onTimeout); |
+ _Future result = new _Future(); |
+ bool completeNormally = true; |
+ Timer timer = new Timer(timeLimit, () { |
+ completeNormally = false; |
+ zone.run(() { |
floitsch
2013/11/27 12:31:16
I think it would be nicer if you passed "onTimeout
Lasse Reichstein Nielsen
2013/11/27 12:54:20
Good point. Done.
|
+ try { |
+ result._complete(onTimeout()); |
+ } catch (e, s) { |
+ result._completeError(e, s); |
+ } |
+ }); |
+ }); |
+ this.then((T v) { |
+ if (completeNormally) { |
+ timer.cancel(); |
+ result._complete(v); |
+ } |
+ }, onError: (e, s) { |
+ if (completeNormally) { |
+ timer.cancel(); |
+ result._completeError(e, s); |
+ } |
+ }); |
+ return result; |
+ } |
} |