Javascript 如何将方法重写为async/await?

Javascript 如何将方法重写为async/await?,javascript,ajax,reactjs,async-await,axios,Javascript,Ajax,Reactjs,Async Await,Axios,在我的react应用程序中,我在axios的帮助下向服务器发出post请求: onSubmit = (results) => { axios.post("http://localhost:8080/simulate/", results) .then( (response) => this.setState({results: response.data.results})) .catch( (error) => this.setState(

在我的react应用程序中,我在axios的帮助下向服务器发出post请求:

 onSubmit = (results) => {
     axios.post("http://localhost:8080/simulate/", results)
      .then( (response) => this.setState({results: response.data.results}))
      .catch( (error) => this.setState({results: error.response.data, hasError: true})
      ); 
  }
如何将此方法重写为
async/await

onSubmit = async (results) => {
  try {
    const response = await axios.post("http://localhost:8080/simulate/", results)
    this.setState({results: response.data.results})
  } catch (error) {
    this.setState({results: error.response.data, hasError: true})
  }
}
编辑-不带Axios 如果您不想使用Axios,可以使用:

编辑-不带Axios 如果您不想使用Axios,可以使用:


您可以使用在Nature中异步的请求承诺您可以使用在Nature中异步的请求承诺我可以不使用axios吗?@leocite您可以使用Fetch api。在这里阅读更多我从服务器收到的响应中收到
OK
,但没有数据您的服务器是否正在返回json我们的纯文本?我可以不使用axios吗?@leocite您可以使用Fetch api。在这里阅读更多我从服务器的响应中收到
OK
,但不是数据您的服务器是否返回json我们的纯文本?
onSubmit = async (results) => {
  try {
    const response = await fetch("http://localhost:8080/simulate/", {
      method: 'POST',
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(results)
    })
    const { data } = await response.json()
    this.setState({results: data.results})
  } catch (error) {
    this.setState({results: error.response.data, hasError: true})
  }
}