【发布时间】:2020-08-07 23:34:14
【问题描述】:
我写了这个简单的代码:
import 'dart:io';
Future<int> f() async {
await Future.delayed(Duration(seconds: 3));
return 7;
}
void main() {
int a = 3;
Future b = f();
b.then( (value) {
print("b value received: " + value.toString() );
a = value;
} );
print(a);
sleep(const Duration(seconds: 6));
print(a);
print("end");
}
函数 f,在辅助线程中,将在 3 秒后返回 7。
在主函数中,我有一个 = 3。
调用函数 f,返回 Future b。
当 b 收到它的值时,我会打印“b value received”,并将 b 赋值给 a。
我所期待的:
- print a -> 3
- after 3 seconds:
- b value received: 7
- after more 3 seconds:
- print a -> 7
- print end
我收到了什么:
- 3
- (after 6 seconds)
- 3
- end
- b value received: 7
我理解错了什么?
【问题讨论】: