简体   繁体   English

如何使 axios get 请求同步?

[英]How can I make an axios get request synchronous?

I'm relatively new to js and am trying to figure out async-await.我对 js 比较陌生,正在尝试弄清楚异步等待。 I am missing something fundamental.我缺少一些基本的东西。 I want to make an HTTP GET that blocks until the response is ready.我想创建一个 HTTP GET 块,直到响应准备好。 I built the code below expecting it to print:我构建了下面的代码,希望它能打印出来:

main1 false "Some Data" main2 main1 false “一些数据” main2

instead it prints:相反,它打印:

main1 true undefined main2 main1 真 未定义 main2

How can I resolve this promise inline?我如何解决这个 promise 内联?

const axios = require('axios');
'use strict'
let URLs= ["http://blah"];
main(process.argv)
function main(argv) {
    console.log('main1');
    const resp = httpgetimpl(URLs[0]);
    console.log(resp instanceof Promise);
    console.log(resp.data);
    console.log('main2');
}
async function httpgetimpl(url) {
    const resp = await axios.get(url);
    return resp;
}

There is no way to make asynchronous operation (like HTTP request) synchronous in JS (there are for sure synchronous API's in node but that's not JS - that's C runtime)没有办法在 JS 中使异步操作(如 HTTP 请求)同步(节点中肯定有同步 API,但那不是 JS - 那是 C 运行时)

Name of the await keyword is a bit misleading but it doesn't mean "stop the execution of whole program here and do nothing until the operation finishes". await关键字的名称有点误导,但它并不意味着“在这里停止整个程序的执行并且在操作完成之前什么都不做”。 It means "return execution flow to my caller (by returning a promise) and when awaited operation is finished, call me back and start executing next line"这意味着“将执行流程返回给我的调用者(通过返回一个承诺),当等待的操作完成时,给我回电话并开始执行下一行”

It's simply syntactic sugar for Promises...它只是 Promises 的语法糖...

Your program rewritten using Promises:使用 Promises 重写的程序:

function httpgetimpl(url) {
    return axios.get(url).then((resp) =>
      // do something with response if you want
      return resp;
    )    
}

function main(argv) {
    console.log('main1');
    const response = httpgetimpl(URLs[0]).then((resp) =>
    {
      // noop
    })
    // following statements are executed before the axios GET request finishes..
    console.log(response instanceof Promise); // true
    console.log(response.data);  // undefined
    console.log('main2');
}

...hopefully you see why it outputs what you see ...希望你明白为什么它会输出你所看到的

If you make your main async and change the call to const resp = await httpgetimpl(URLs[0]);如果您使main async并将调用更改为const resp = await httpgetimpl(URLs[0]); this is the code using Promises:这是使用 Promises 的代码:

function httpgetimpl(url) {
    return axios.get(url).then((resp) =>
      // do something with response if you want
      return resp;
    )    
}

function main(argv) {
    console.log('main1');
    return httpgetimpl(URLs[0]).then((resp) =>
    {
      console.log(resp instanceof Promise);
      console.log(resp.data);
      console.log('main2');
    })
}

...which should print the expected ...应该打印预期的

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

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