简体   繁体   English

js 新手 - 了解 async await 语法

[英]new to js - understanding async await syntax

I am attempting a basic concept that is still eluding me... In the option (that is commented out) including the Start().then... I'm able to nest the functions so that they start and end in the desired order.我正在尝试一个仍然让我难以理解的基本概念......在包括 Start().then 在内的选项(已注释掉)中......我能够嵌套函数,以便它们以所需的方式开始和结束命令。 In the await version, they start & end in the appropriate order but I'm not clear how I'm meant to put in log the resolve text after each one has completed.在等待版本中,它们以适当的顺序开始和结束,但我不清楚我打算如何在每个完成后输入解析文本。 Sorry this is basic...对不起,这是基本...

console.log("Synchronous result.");

function Start() {
  return new Promise(function (resolve) {
    console.log(`Starting the Start`);
    setTimeout(() => resolve("Start has finished"), 5000);
  });
}

function Middle() {
  return new Promise(function (resolve) {
    console.log(`Starting the Middle`);
    setTimeout(() => resolve("Middle has finished"), 2000);
  });
}

function End() {
  return new Promise(function (resolve) {
    console.log(`Starting the End`);
    setTimeout(() => resolve("End has finished"), 1000);
  });
}

// this works in the traditional promise method

/*
Start().then((result) => {
  console.log(result),
    Middle().then((result) => {
      console.log(result),
        End().then((result) => {
          console.log(result);
        });
    });
});
*/

// now trying async/await
async function workflow() {
  let call1 = await Start();
  let call2 = await Middle();
  let call3 = await End();
}

workflow();

Well, you almost have it.好吧,你几乎拥有它。 In an async function, the await will return the result of the promise.在异步 function 中, 等待将返回 promise 的结果。 So all you have to do is do the console.log after.所以你所要做的就是在之后执行console.log。

 console.log("Synchronous result."); function Start() { return new Promise(function (resolve) { console.log(`Starting the Start`); setTimeout(() => resolve("Start has finished"), 5000); }); } function Middle() { return new Promise(function (resolve) { console.log(`Starting the Middle`); setTimeout(() => resolve("Middle has finished"), 2000); }); } function End() { return new Promise(function (resolve) { console.log(`Starting the End`); setTimeout(() => resolve("End has finished"), 1000); }); } // this works in the traditional promise method /* Start().then((result) => { console.log(result), Middle().then((result) => { console.log(result), End().then((result) => { console.log(result); }); }); }); */ // now trying async/await async function workflow() { let call1 = await Start(); console.log(call1); let call2 = await Middle(); console.log(call2); let call3 = await End(); console.log(call3); } workflow();

The resolve method of the new Promise you can imagine as a return value. new Promiseresolve方法你可以想象为一个return值。 In this case you returning strings, what gets assigned to the variables call1 call2 call3 .在这种情况下,您返回字符串,分配给变量call1 call2 call3的内容。 You can resolve any valid javascript data structure.您可以解析任何有效javascript数据结构。

// now trying async/await
async function workflow() {
  let call1 = await Start();
  console.log("call1: ", call1);
  let call2 = await Middle();
  console.log("call2: ", call2);
  let call3 = await End();
  console.log("call3: ", call3);
}

workflow();

As you get a bit more hands on, you can also discover the Promise.all() and Promise.allSettled() .随着您的更多操作,您还可以发现Promise.all()Promise.allSettled()

The async/await is the moder way of Promise usage. async/awaitPromise使用的现代方式。 Keep it like this.保持这样。

await XXX evaluates to be the resolved value of the promise XXX. await XXX评估为 promise XXX 的解析值。

So the variables you've named callX contain the resolved values.因此,您命名为callX的变量包含已解析的值。

 console.log("Synchronous result."); function Start() { return new Promise(function (resolve) { console.log(`Starting the Start`); setTimeout(() => resolve("Start has finished"), 5000); }); } function Middle() { return new Promise(function (resolve) { console.log(`Starting the Middle`); setTimeout(() => resolve("Middle has finished"), 2000); }); } function End() { return new Promise(function (resolve) { console.log(`Starting the End`); setTimeout(() => resolve("End has finished"), 1000); }); } // this works in the traditional promise method // now trying async/await async function workflow() { let call1 = await Start(); console.log({call1}); let call2 = await Middle(); console.log({call2}); let call3 = await End(); console.log({call3}); } workflow();

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

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