关于 Dart 中 Future 的问题
Questions about Future in Dart
异步编程教程here talks about async
await
, but avoids discussing the Future
API. I could find more information about the Future
API here,但我还有一些疑问。这些教程让我提出了一些问题,理想情况下我应该为每个问题放置一个问题,但由于它们很小且相关,我更愿意在一个地方提出所有问题。
什么triggers/starts一个Future
执行?
从文本中我只能得出结论,一旦 async
方法 returns Future
将立即被运行时触发。
Future.wait()
和Future.then().then().then()
有什么区别?
return await myFuture;
和return myFuture
一样吗?
文中说 async
方法在遇到 await
或 return
时将 return 不完整 Future
。
文字说:
Important: Async functions return Futures. If you don’t want your function to return a future, then use a different solution. For example, you might call an async function from your function.
我们如何调用一个 async
函数,得到它的 return 值,而不是 await
,从而不是一个 async
函数?
What triggers/starts a Future execution?
Future
的代码块被放入事件队列中,因此在事件队列中轮到它时执行。 Flutter in Focus Futures 视频对它的工作原理有一些很好的视觉效果。
What's the difference between Future.wait() and Future.then().then().then()?
- 它们是处理多个 futures 的不同方式,但差异很微妙。
Future.wait
处理错误的方式略有不同,它的值被 return 编辑为列表而不是顺序代码块,但在实践中,差异可能对您的情况无关紧要。
Are return await myFuture;
and return myFuture
the same?
没有。在第一个实例中,执行会在 await 处暂停,直到 Future
在事件队列中得到处理。在第二个实例中,Future
本身被 returned 给调用者并且执行按照调用者的意愿继续(可能直到 Future
有机会被处理)。
await
是一种语言特性,本质上是在等待 Future 在那一点完成,而 return
只是 return 将 Future
本身发送给调用者。
How can we call an async
function, get its return value, and not await
, thus, not be an async
function?
- 如果您需要 return 值,那么您将调用
async
函数,然后直接使用它的 Future
而不是使用 await
。这是一个愚蠢的例子:
Future<int> getAsyncInt() async {
return 0;
}
void testAsync() {
getAsyncInt().then((value) {
print("Got an async int: $value");
});
}
在上面的例子中,你可以看到我们使用了一个异步函数,但是testAsync
并没有等待值(而是使用了一个Future,所以最终结果是一样的)。
如果您不需要 return 值,您可以直接调用 async
函数:
Future<int> getAsyncInt() async {
return 0;
}
void testAsync() {
getAsyncInt();
}
在第二种情况下,实际上 getAsyncInt()
确实会被调用,即使它的 return 值被调用者忽略。
这些都是很好的问题,顺便说一句,希望对您有所帮助。 async/await
可能相当神秘,但将它们视为事件队列确实有助于理解执行流程恕我直言。
异步编程教程here talks about async
await
, but avoids discussing the Future
API. I could find more information about the Future
API here,但我还有一些疑问。这些教程让我提出了一些问题,理想情况下我应该为每个问题放置一个问题,但由于它们很小且相关,我更愿意在一个地方提出所有问题。
什么triggers/starts一个
Future
执行?从文本中我只能得出结论,一旦
async
方法 returnsFuture
将立即被运行时触发。Future.wait()
和Future.then().then().then()
有什么区别?return await myFuture;
和return myFuture
一样吗?文中说
async
方法在遇到await
或return
时将 return 不完整Future
。文字说:
Important: Async functions return Futures. If you don’t want your function to return a future, then use a different solution. For example, you might call an async function from your function.
我们如何调用一个 async
函数,得到它的 return 值,而不是 await
,从而不是一个 async
函数?
What triggers/starts a Future execution?
Future
的代码块被放入事件队列中,因此在事件队列中轮到它时执行。 Flutter in Focus Futures 视频对它的工作原理有一些很好的视觉效果。
What's the difference between Future.wait() and Future.then().then().then()?
- 它们是处理多个 futures 的不同方式,但差异很微妙。
Future.wait
处理错误的方式略有不同,它的值被 return 编辑为列表而不是顺序代码块,但在实践中,差异可能对您的情况无关紧要。
Are
return await myFuture;
andreturn myFuture
the same?
没有。在第一个实例中,执行会在 await 处暂停,直到
Future
在事件队列中得到处理。在第二个实例中,Future
本身被 returned 给调用者并且执行按照调用者的意愿继续(可能直到Future
有机会被处理)。await
是一种语言特性,本质上是在等待 Future 在那一点完成,而return
只是 return 将Future
本身发送给调用者。
How can we call an
async
function, get its return value, and notawait
, thus, not be anasync
function?
- 如果您需要 return 值,那么您将调用
async
函数,然后直接使用它的Future
而不是使用await
。这是一个愚蠢的例子:
Future<int> getAsyncInt() async {
return 0;
}
void testAsync() {
getAsyncInt().then((value) {
print("Got an async int: $value");
});
}
在上面的例子中,你可以看到我们使用了一个异步函数,但是testAsync
并没有等待值(而是使用了一个Future,所以最终结果是一样的)。
如果您不需要 return 值,您可以直接调用 async
函数:
Future<int> getAsyncInt() async {
return 0;
}
void testAsync() {
getAsyncInt();
}
在第二种情况下,实际上 getAsyncInt()
确实会被调用,即使它的 return 值被调用者忽略。
这些都是很好的问题,顺便说一句,希望对您有所帮助。 async/await
可能相当神秘,但将它们视为事件队列确实有助于理解执行流程恕我直言。