这个 async-await 方法调用究竟是如何工作的?这是正确的吗? [英] How exactly works this async-await method call? is it correct?

查看:19
本文介绍了这个 async-await 方法调用究竟是如何工作的?这是正确的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Angular 和异步编程不太感兴趣,我有以下几点.

I am not so into Angular and async programming and I have the following doub.

进入一个服务类我有这个异步方法:

Into a service class I have this async method:

  async aestheticEvaluationExist(patientUID: string) {
    console.log("patientUID: ", patientUID);
    //return this.firestore.collection('aestetic-evaluation').doc(patientUID).get();   

    //const docRef = (await this.firestore.collection('aestetic-evaluation').doc(patientUID).);
    const exists: boolean = (await this.firestore.collection('aesthetic-evaluation').doc(patientUID).get().toPromise()).exists;

    console.log("aesteticEvaluationExist() exists: ", exists);

    return exists;
  }

然后进入我的组件类,我有这个方法调用以前的服务方法::

Then into my component class I have this method calling the previous service method::

let isUpdate = true;
(await this.patientService.aestheticEvaluationExist(this.patientUID)
                   .then(res => {
                                  console.log("RES: ", res);
                                  isUpdate = res;
                                }));

console.log("isUpdate: ", isUpdate); 

如您所见,它在方法调用前有 awayt 关键字.

As you can see it have awayt keyword before method call.

它似乎工作正常(输出是正确的)事实上到 then() 我的 res 是假的,当我通过最后一个 console.log() 行打印它时它打印 false,表示原真值被正确覆盖.

It seems to works fine (the output is correct) infact into the then() my res is false and when I print it by the last console.log() line it print false, it means that the original true value was correctly overwritten.

我问这个 async 服务方法定义是否在方法调用上使用 await 关键字确保我的最后一个 console.log() 行在我的方法完成后执行.

I am aksing if this async service method definition with the await keyword on the method call it ensure me that my last console.log() line is executed after that my method completed.

我的理解正确吗?或者为了确保正确的结果,我必须在 then() 中执行最后一个操作?

Is it my understanding correct? Or to ensure the correct result I have to perform this last operation inside the then()?

推荐答案

我问这个异步服务方法定义是否在方法调用上带有 await 关键字,它确保我的最后一个 console.log() 行在我的方法完成后执行.

I am aksing if this async service method definition with the await keyword on the method call it ensure me that my last console.log() line is executed after that my method completed.

是的,大致.async/await 是用于创建和使用 Promise 的语法.async 函数返回一个承诺.当 async 函数中的代码到达第一个 await 时,该函数返回其承诺,等待代码使用的 await 解决,并且然后继续执行函数逻辑,最终解决 async 函数返回的承诺.

Yes, roughly. async/await is syntax for creating and consuming promises. An async function returns a promise. When code in the async function reaches the first await, the function returns its promise, waits for what the code used await on to settle, and then continues with the function logic, eventually settling the promise the async function returned.

所以函数的逻辑不会进展到那个 console.log,直到你 await 的承诺得到解决.

So the logic of the function won't progress to that console.log until the promise you're awaiting is settled.

请注意,很少有理由将诸如 thencatch 之类的 promise 方法的显式调用与 async/await 语法.使用其中之一.(也不需要在 () 中包装 await 运算符和操作数.)在这种情况下,您可以替换:

Note that there's rarely a reason to combine explicit calls to promise methods like then and catch with async/await syntax. Use one or the other. (There's also no need to wrap the await operator and operand in ().) In this case, you can replace this:

let isUpdate = true;
(await this.patientService.aestheticEvaluationExist(this.patientUID)
    .then(res => {
        console.log("RES: ", res);
        isUpdate = res;
    }));
console.log("isUpdate: ", isUpdate); 

let isUpdate = true;
const res = await this.patientService.aestheticEvaluationExist(this.patientUID);
console.log("RES: ", res);
isUpdate = res;
console.log("isUpdate: ", isUpdate); 

甚至可能

let isUpdate = await this.patientService.aestheticEvaluationExist(this.patientUID);
console.log("isUpdate: ", isUpdate); 

这篇关于这个 async-await 方法调用究竟是如何工作的?这是正确的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆