简体   繁体   English

我可以在没有.then的情况下使用async等待吗?

[英]Can I use async await without .then?

I'm a bit confused about the MDN documentation of async await . 我对async awaitMDN文档有些困惑。 These docs use the .then() syntax to respond when an async function has resolved: 这些文档使用.then()语法在async功能解决后做出响应:

mdn example mdn示例

async function add() {
  const b = await resolveAfter2Seconds(30); // timer
  return b;
}
add().then(v => {
  console.log(v);  
});

But in my own code, I don't use .then() and it still works asynchronously. 但是在我自己的代码中,我不使用.then() ,它仍然可以异步工作。 The flow is not blocked. 流没有被阻塞。 So why would you use .then() ? 那么,为什么要使用.then()呢?

async code without .then 没有.then的异步代码

function start(){
    console.log("starting!")
    let d = loadData()
    console.log("this message logs before loadData returns!")
    console.log(d)   // this shows: Promise - Pending because d has not yet returned
}
async function loadData() {
    const response = await fetch("https://swapi.co/api/films/");
    const json = await response.json();
    console.log("data loaded!")
    return json;
}

First of all, all async functions return a Promise, so if you want to get the returned value from that async operation, you'll either need to use then or await inside an async function. 首先,所有异步函数都返回一个Promise,因此,如果要从该异步操作中获取返回的值, then需要使用then或在异步函数中await

MDN uses .then because the add async function is being called outside an async function scope, so it can't use await globally to catch the data from the promise. MDN使用.then因为add async函数在async函数作用域之外被调用,因此它不能全局使用await来捕获来自promise的数据。

In your example, you get the same, an instance of Promise as the return of your loadData async function, if you define the start function also as async, you can use let d = await loadData() , if it's not async, you can use .then (which is the Promise API ). 在您的示例中,您将获得一个与Promise相同的实例,作为您的loadData异步函数的返回,如果您还将start函数也定义为异步,则可以使用let d = await loadData() ,如果它不是异步的,则可以使用.then (这是Promise API )。

Async function declaration async function loadData() returns AsyncFunction which is executed asynchronously and always returns a Promise . 异步函数声明async function loadData()返回AsyncFunction ,它异步执行,并且始终返回Promise

Basically all the code you put inside the async function someFunctionName will be executed inside that Promise and when you return some value inside that function – it will resolve that promise. 基本上,您放入async function someFunctionName所有代码都将在该Promise执行,并且当您在该函数中return一些值时,它将解决该诺言。

So that then() call is to get the actual value from that Promise that your function returned. 因此, then()调用就是从函数返回的Promise中获取实际值。

Your code works because it is not returning promise object and actually waiting for the response. 您的代码之所以有效,是因为它没有返回Promise对象,而是实际上在等待响应。 It returns the json at the end. 它在末尾返回json。 So, execution holds till it gets the response. 因此,执行一直保持到得到响应为止。

If your function is async you don't need to return promise, your return statement will wait for all 'await' statement before it to complete. 如果您的函数是异步的,则不需要返回promise,您的return语句将等待所有'await'语句完成。

您自己的代码有效,因为您将.then()的用法替换为async await的用法。

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

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