这个async-await方法调用到底如何工作?这是正确的吗?

这个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为false,当我在最后一个console.log()行中打印时,它打印false ,这表示原始的true值已被正确覆盖.

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.

我想知道是否在此方法上使用 await 关键字的 async 服务方法调用确保我最后一个 console.log()在我的方法完成后执行了strong>行.

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 函数返回一个Promise.当 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.

请注意,几乎没有理由将对诸如 then catch 之类的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);