How to force a program to wait until an HTTP request is finished in JavaScript?

前端 未结 6 450
孤城傲影
孤城傲影 2020-12-25 12:41

Is there a way in JavaScript to send an HTTP request to an HTTP server and wait until the server responds with a reply? I want my program to wait until the server replies an

6条回答
  •  自闭症患者
    2020-12-25 13:25

    For those using axios, you can wrap it in an async iife and then await it:

    (async () => {
      let res = await axios.get('https://example.com');
    
      // do stuff with the response
    })();
    

    Note, I haven't done any error checking here.

提交回复
热议问题