The await
and async
keywords were introduced in Dart 1.9.4 in order to simplify asynchronous operations. Consider the following call to a method that returns Future
:
obj.method().then(handlerFunction)
This is fine for the post part, but what if things get more complicated and handlerFunction
returns a future too?
obj.method().then(handlerFunction).then(handlerFunction2);
Things are starting to get complicated already—debugging is not straightforward. Ideally, we would want to deal with one part of the chain at a time and hold up the execution of statements until a desired operation is complete. This is what await
allows:
var f1 = await obj.aMethod(); var result = await f1.aMethod();
Functions and methods to be called with await
return Future
. They must also be declared as async
in the method's header, as does the function using the await
call:
class Foo{ Future<int> aMethod() async { return await aFunction(); } }
The async
and await
keywords...