0

I want to make multiple requests with Axios without waiting it finish one on one.

what i want to do is, even when the first request is waiting, i want the second request is keep proccess and send the return, i dont want to waiting the first request because it will take long time..

async sendLog() {
    setTimeout(() => {
    if (this.proccessData === true) { 
        $nuxt.$axios.post('http://localhost/api/log/procreate', {logFile: this.form.logName})
        .then((res) => {
            this.sendLog();
        })
    }
    }, 1000);
},
async addServer() {      
   $nuxt.$axios.post('http://localhost/api/servers/create', this.form)
    .then((res) => {
      this.proccessData = false;
    }
}

But this code will run the next request when the current request is finished.

Jazuly
  • 1,374
  • 5
  • 20
  • 43
  • 1
    Does this answer your question? [How to post multiple Axios requests at the same time?](https://stackoverflow.com/questions/61385454/how-to-post-multiple-axios-requests-at-the-same-time) – Simone Rossaini Jul 02 '20 at 07:51
  • Why is this tagged with PHP and Laravel when it's a question about JS/Axios? – M. Eriksson Jul 02 '20 at 07:51
  • @MagnusEriksson cause i use laravel as backend, maybe theres a setting on backend site.. – Jazuly Jul 02 '20 at 07:52
  • Well, since it's about calling the backend, it would be odd if you needed to call the backend to get the settings for how to call the backend :-) So no, PHP and Laravel is unrelated here. – M. Eriksson Jul 02 '20 at 07:55
  • @SimoneRossaini still waiting the first finish.. what i want to do is, even when the first request is waiting, i want the second request is keep proccess and send the return, i dont want to waiting the first cause it will take long time... – Jazuly Jul 02 '20 at 08:05

2 Answers2

1
const foo = async() => {
  const p1 = $nuxt.$axios.post();
  const p2 = $nuxt.$axios.post();
  const [res1, res2] = await Promise.all([p1, p2]);
  // do with res1 and res2
}

see Promise.all

sodust
  • 11
  • 2
0

Not really sure if I understand your question correctly. If you want sendLog to be processed multiple times without waiting the previous one to finish, you might choose to use interval as following

async sendLog() {
  setInterval(() => {
    if (this.proccessData === true) {
      $nuxt.$axios.post('http://localhost/api/log/procreate', {
        logFile: this.form.logName
      });
    }
  }, 1000);
}
Thinh Tran
  • 146
  • 6
  • no problem with the method, the problem is request from method `sendlog` not proccess when request from method `addserver` still running.. – Jazuly Jul 02 '20 at 08:26
  • I don't see any reason why request from addServer could affect the request from sendLog. Unless you are reached to the concurrent requests limit of the browser or `this.proccessData` is incorrectly setting elsewhere. – Thinh Tran Jul 02 '20 at 10:15