处理 Futures
Future checkVersion() async {
var version = await lookUpVersion();
// Do something with version
}
try {
version = await lookUpVersion();
} catch (e) {
// React to inability to look up the version
}
- 可以在一个 async 函数中多次使用 await
var entrypoint = await findEntrypoint();
var exitCode = await runExecutable(entrypoint, args);
await flushThenExit(exitCode);
声明 async 函数
// 同步函数
String lookUpVersion() => '1.0.0';
// 异步函数
Future<String> lookUpVersion() async => '1.0.0';
处理 Streams
await for (varOrType identifier in expression) {
// Executes each time the stream emits a value.
}
await for
必须在 async 的函数体中
Future main() async {
// ...
await for (var request in requestServer) {
handleRequest(request);
}
// ...
}