Dart 是一门单线程编程语言!!
Future
Future 对象封装了Dart 的异步操作,它有未完成(uncompleted)和已完成(completed)两种状态。
completed状态也有两种:一种是代表操作成功,返回结果;另一种代表操作失败,返回错误。
import 'dart:io';
// 同步的方式
// run() {
// sleep(Duration(seconds: 2)); //延迟2s后输出
// return 'cookie';
// }
// void main(List<String> arguments) {// 入口函数
// print('strat');
// var res = run();
// print(res);
// print('end');
// }
// 异步的方式
Future run() {
sleep(Duration(seconds: 2)); //延迟2s后输出
return Future(() => 'cookie');
}
void main() async{// 入口函数
print('strat');
// var f = run();
// f.then((res) => print(res)).catchError((e) => print(e)); // 这样的方式不利于赋值
var res = await run(); // 方便赋值
print(res);
print('end');
}