From 55e5c4d026113b48f9e89edf1ee923e36568f4e8 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Thu, 22 Mar 2018 10:52:50 -0700 Subject: [PATCH] Slightly improve the docs for the internal _futurize method (#4847) --- lib/ui/painting.dart | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart index 2412101c5..d69314bbb 100644 --- a/lib/ui/painting.dart +++ b/lib/ui/painting.dart @@ -2881,32 +2881,35 @@ typedef String _Callbacker(_Callback callback); /// Converts a method that receives a value-returning callback to a method that /// returns a Future. /// +/// Return a [String] to cause an [Exception] to be sychnorously thrown with that +/// string as a message. +/// +/// If the callback is called with null, the future completes with an error. +/// /// Example usage: +/// /// ```dart /// typedef void IntCallback(int result); /// -/// void doSomethingAndCallback(IntCallback callback) { +/// String _doSomethingAndCallback(IntCallback callback) { /// new Timer(new Duration(seconds: 1), () { callback(1); }); /// } /// /// Future doSomething() { -/// return _futurize(domeSomethingAndCallback); +/// return _futurize(_doSomethingAndCallback); /// } /// ``` -/// Future _futurize(_Callbacker callbacker) { final Completer completer = new Completer.sync(); - final String err = callbacker((T t) { + final String error = callbacker((T t) { if (t == null) { completer.completeError(new Exception('operation failed')); } else { completer.complete(t); } }); - - if (err != null) - throw new Exception(err); - + if (error != null) + throw new Exception(error); return completer.future; } -- GitLab