prefer-async-await
Recommends to use async/await syntax to handle Futures result instead of .then()
invocation. Also can help prevent errors with mixed await
and .then()
usages, since awaiting the result of a Future
with .then()
invocation awaits the completion of .then()
.
Example
❌ Bad:
Future<void> main() async {
// LINT: Prefer async/await syntax instead of '.then' invocations.
// Try rewriting to async/await.
someFuture.then((result) => handleResult(result));
// LINT: Prefer async/await syntax instead of '.then' invocations.
// Try rewriting to async/await.
await (foo.asyncMethod()).then((result) => handleResult(result));
}
✅ Good:
Future<void> main() async {
final result = await someFuture;
handleResult(result);
final anotherResult = await foo.asyncMethod();
handleResult(anotherResult);
}