简体   繁体   English

嵌套的异步/等待函数

[英]Nested async/await functions

I am trying to understanding how to use async/await in nested manner.我试图了解如何以嵌套方式使用async/await In the example, I am calling function a() and inside that, I want to call b() and ensure that b() resolves before a().在示例中,我调用 function a(),在其中,我想调用 b() 并确保 b() 在 a() 之前解析。 The following is the code and although b() is called, it is not getting resolved.以下是代码,虽然 b() 被调用,但它没有得到解决。

 function a() { b(); return new Promise((resolve, reject) => { setTimeout(() => { resolve('resolved a'); }, 2000); }); } function b() { console.log('calling b'); return new Promise((resolve, reject) => { setTimeout(() => { resolve('resolved b'); }, 2000); }); } async function asyncCall() { console.log('calling'); const result = await a(); console.log(result); } asyncCall();

The output of the code above is:上面代码的output是:
"calling" “呼唤”
"calling b" “打电话给b”
"resolved a" (This line shows up after 2 seconds ) “解决了”(这条线在2 秒后出现)

Then, I changed to code to include async/await in a(), (everything else is the same)然后,我更改了代码以在 a() 中包含 async/await,(其他一切都相同)

 async function a() { await b(); return new Promise((resolve, reject) => { setTimeout(() => { resolve('resolved a'); }, 2000); }); } function b() { console.log('calling b'); return new Promise((resolve, reject) => { setTimeout(() => { resolve('resolved b'); }, 2000); }); } async function asyncCall() { console.log('calling'); const result = await a(); console.log(result); } asyncCall();

The output of the code above is:上面代码的output是:
"calling" “呼唤”
"calling b" “打电话给b”
"resolved a" (This line shows up after 4 seconds [In the previous example it was 2 seconds]) "resolved a" (这条线在4 秒后出现 [在前面的例子中是 2 秒])

But in both the case, ' resolved b ' is not getting printed.但在这两种情况下,' resolved b ' 都没有被打印出来。

I have 2 questions:我有两个问题:

  1. How do I print ' resolved b '?如何打印“已解决的 b ”?
  2. Is it common to have cases like this where an async function (in this case, function "a"), is both the callee (it is called by asyncCall()) and the caller (it is calling b())?是否常见这样的情况:异步 function(在本例中为 function “a”)既是被调用者(它由 asyncCall() 调用)又是调用者(它正在调用 b())?

How do I print 'resolved b'?如何打印“已解决的 b”?

Your code as it stands completely ignores the resolved value of b .您的代码完全忽略了b的解析值。 The first example ignores the promise entirely.第一个示例完全忽略了 promise。 The second example waits for the promise to resolve but does nothing with the resolved value (not even capture it in a variable).第二个示例等待 promise 解析,但对解析的值不做任何事情(甚至不将其捕获在变量中)。

You have to console.log() it like you did everything else you logged.您必须console.log()它就像您记录的所有其他内容一样。

You could logthisvalue = await b() .您可以logthisvalue = await b() You could b().then(logthisvalue => here) .你可以b().then(logthisvalue => here) You could modify b() so it logs its own value.您可以修改b()以便它记录自己的值。

Is it common to have cases like this where an async function (in this case, function "a"), is both the callee (it is called by asyncCall()) and the caller (it is calling b())?是否常见这样的情况:异步 function(在本例中为 function “a”)既是被调用者(它由 asyncCall() 调用)又是调用者(它正在调用 b())?

Yes.是的。

The async and await keywords are tools to manage promises. asyncawait关键字是管理 Promise 的工具。

Top level await has poor support so, in general, you can only use await inside an async function.顶级await的支持很差,所以一般来说,您只能在async function 中使用await

You have to call an async function for it to do anything.您必须调用async function 才能执行任何操作。

The async function has to get a promise to await from somewhere and usually that is by calling another function. async function 必须让 promise 从某个地方await ,通常是通过调用另一个 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM