Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asynchronous 为什么在下面的程序中异步函数在同步之前被调用?_Asynchronous_Dart_Async Await_Dart Pub - Fatal编程技术网

Asynchronous 为什么在下面的程序中异步函数在同步之前被调用?

Asynchronous 为什么在下面的程序中异步函数在同步之前被调用?,asynchronous,dart,async-await,dart-pub,Asynchronous,Dart,Async Await,Dart Pub,showsync函数没有执行任何需要等待的操作,因此它只是执行。如果更改为以下内容,则其他功能将首先打印: Async Function Call!! Sync Function Call! all done!! 正如Günter在评论中指出的:“在Dart 1.x中,异步函数立即暂停执行。在Dart 2中,异步函数不是立即暂停,而是同步执行,直到第一次等待或返回。”(引用自Dart文档) 因此,如果您添加第二个wait showsync(),它将不会在同步调用之前执行 此处提供了详细说明:右

showsync
函数没有执行任何需要等待的操作,因此它只是执行。如果更改为以下内容,则其他功能将首先打印:

Async Function Call!!
Sync Function Call!
all done!!
正如Günter在评论中指出的:“在Dart 1.x中,异步函数立即暂停执行。在Dart 2中,异步函数不是立即暂停,而是同步执行,直到第一次等待或返回。”(引用自Dart文档)

因此,如果您添加第二个
wait showsync()
,它将不会在同步调用之前执行


此处提供了详细说明:

右侧。在Dart 2中,异步函数开头的同步部分是同步执行的。@GünterZöchbauer谢谢,我在答案中添加了一些信息。
Async Function Call!!
Sync Function Call!
all done!!
showAsync() {
  Future.delayed(Duration(seconds: 1), () {
    print('Async Function Call!!');
  });
}