Asynchronous 在Dart异步方法中,某些行不能';不共享任何依赖项是否可以异步运行?
假设您有一个异步方法体,如下所示,其中包含一个方法调用,该方法调用返回一个Future,随后是一个print,它不需要该方法的结果。是否可以添加一些结构以使print语句独立执行?或者,这种语法糖是否强制异步方法体完全按顺序运行Asynchronous 在Dart异步方法中,某些行不能';不共享任何依赖项是否可以异步运行?,asynchronous,dart,Asynchronous,Dart,假设您有一个异步方法体,如下所示,其中包含一个方法调用,该方法调用返回一个Future,随后是一个print,它不需要该方法的结果。是否可以添加一些结构以使print语句独立执行?或者,这种语法糖是否强制异步方法体完全按顺序运行 Future<String> test2() { Completer c = new Completer(); new Timer(new Duration(seconds: 2), () { c.complete('test2: done
Future<String> test2() {
Completer c = new Completer();
new Timer(new Duration(seconds: 2), () {
c.complete('test2: done');
});
return c.future;
}
Future<String> test1() async {
String result = await test2();
print(result);
// Why is this not executed immediately before test2 call completes?
print('hello');
return 'test1: done';
}
void main() {
test1().then((result) => print(result));
}
Future test2(){
完成符c=新的完成符();
新计时器(新持续时间(秒数:2),(){
c、 完成(“测试2:完成”);
});
返回c.未来;
}
Future test1()异步{
字符串结果=等待test2();
打印(结果);
//为什么在test2调用完成之前不立即执行此操作?
打印(“你好”);
返回“test1:done”;
}
void main(){
test1()。然后((结果)=>打印(结果));
}
后续:我在下面添加了test1()的重写,它链接了异步方法调用。我真正想知道的是如何使用异步语法糖来简化这种类型的用例。如何用新语法重写此块
Future<String> test1() async {
test2().then((result) {
print(result);
test2().then((result) {
print(result);
});
});
// This should be executed immediately before
// the test2() chain completes.
print('hello');
return 'test1: done';
}
Future test1()异步{
test2()。然后((结果){
打印(结果);
test2()。然后((结果){
打印(结果);
});
});
//这项工作应在开工前立即执行
//test2()链完成。
打印(“你好”);
返回“test1:done”;
}
这是因为在test2()之前有wait
代码>。执行暂停,直到test2()
返回的未来完成
后续编辑:
我不确定你到底想做什么,但如果你想在打印东西之前等待链完成,以下是你必须做的:
Future<String> test1() async {
String result;
var result1 = await test2();
print("Printing result1 $result1 ; this will wait for the first call to test2() to finish to print");
var result2 = await test2();
print("Printing result2 $result2 ; this will wait for the second call to test2() to print");
// This should be executed immediately before
// the test2() chain completes.
print('hello');
return 'test1: done';
}
如果您希望独立于以前的将来结果执行代码,只需不使用wait
关键字即可
Future<String> test1() async {
test2().then((String result) => print(result)); // the call to test2 will not stop the flow of test1(). Once test2's execution will be finished, it'll print the result asynchronously
// This will be printed without having to wait for test2 to finish.
print('hello');
return 'test1: done';
}
Future test1()异步{
test2()。然后((字符串结果)=>print(结果));//对test2的调用不会停止test1()的流。一旦test2的执行完成,它将异步打印结果
//这将在不必等待test2完成的情况下打印出来。
打印(“你好”);
返回“test1:done”;
}
关键字await
使流暂停,直到test2()
完成。如果您不想实际等待,为什么要使用await?我的印象是,await会暂停执行需要test2结果的后续代码。但是由于print('hello')
不需要这个结果,我认为它应该能够独立执行。这只是一个简单的例子,但是如果方法体内容被放大到有几个块可以独立执行,我想知道如何添加代码来支持它。
Future<String> test1() async {
test2().then((String result) => print(result)); // the call to test2 will not stop the flow of test1(). Once test2's execution will be finished, it'll print the result asynchronously
// This will be printed without having to wait for test2 to finish.
print('hello');
return 'test1: done';
}