简体   繁体   English

然后不等待 Promise

[英]then not waiting for Promise

I have this function using fetch我有这个 function 使用 fetch

export async function login(email, password) {
  let url = new URL("http://localhost:8080");
  url.search = new URLSearchParams({
    t: "login",
    email: email,
    password: password
  });

  fetch(url, {
    method: "GET",
    headers: new Headers()
  })
    .then(response => response.text())
    .then(response => {
      console.log(response);
      return response;
    })
    .catch(err => console.log(err));
}

And the function that's supposed to call it and handle the result is this应该调用它并处理结果的 function 是这样的

handleSubmit() {
    login(this.state.email, this.state.password).then(r => {
      console.log(r);
      if (String(r) === "true") this.props.updatePath("/main");
      else this.setState({ current: 1 });
    });
  }

I'm new to javascript, and I'm still a bit confused with async thing.我是 javascript 的新手,我仍然对异步的东西有点困惑。 In the console, the log from the second comes before the log from the first function, so I'm assuming it's not waiting for it to function.在控制台中,第二个的日志在第一个 function 的日志之前,所以我假设它没有等待它到 function。

The log on the first function spits out the correct result, I just can't get it to the other side.第一个function上的日志吐出正确的结果,我就是拿不到对面。

I was using axios at first (had some problems it with) and this approch worked.起初我使用的是 axios (遇到了一些问题),这个方法奏效了。 Is there something different about fetch that stops this from working? fetch 有什么不同之处可以阻止它工作吗? Any help would be appreciated任何帮助,将不胜感激

The Promise that comes from calling an async function resolves when that function returns.来自调用async function 的 Promise 在 function 返回时解析。 login internally doesn't await anything (that's probably a mistake).内部login不会等待任何事情(这可能是一个错误)。 It returns before the internal fetch resolves.它在内部fetch解决之前返回。 So while that fetch is in flight, your then callback in handleSubmit runs.因此,当该获取正在进行时,您在handleSubmit中的then回调运行。 Later the fetch initiated in login finishes and logs the actual response.稍后login中启动的fetch完成并记录实际响应。

I would have you try writing login like this:我会让你尝试这样写login

export async function login(email, password) {
  let url = new URL("http://localhost:8080");
  url.search = new URLSearchParams({
    t: "login",
    email: email,
    password: password
  });

  try {
    let response = await fetch(url, {
      method: "GET",
      headers: new Headers()
    });
    response = await response.text();
    console.log(response);
    return response;
  } catch (err) {
    console.log(err);
    // original code had this issue where it doesn't reject on error nor resolve to anything useful
  }
}

await is very simple concept - it just makes your function wait for the Promise in synchronous manner. await是一个非常简单的概念 - 它只是让您的 function 以同步方式等待 Promise。 In order to make use of it, put await before any expression that returns a Promise.为了使用它,请将await放在任何返回 Promise 的表达式之前。 In your case, you can return fetch() in your login() function and then just:在您的情况下,您可以在login() function 中返回fetch() ,然后只需:

await login(this.state.email, this.state.password);
// execution continued when fetch is done

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

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