How to make multiples ajax calls simultaneously and asynchronously

Solution for How to make multiples ajax calls simultaneously and asynchronously
is Given Below:

I have a javascript object called Agendamento that has these important parts:

const Agendamento = {
   // ...

   storeResultados: async function (consulta) {
        //...
        $.ajax({
            type: 'POST',
            url: '<my-url>',
            data: <my-data>,
        });
    }
}

and in another place I have this another important parte:

for (const consulta of consultas) {
    Agendamento.storeResultados(consulta);
}

my question is: WHY ARE THE CALLS NOT BEING CALLED SIMULTANEOUSLY AND ASYNCHRONOUSLY??.

THE PURPOSE OF THIS: As you may guess, these APIs that I’m calling take too much time to return data. So I know that these calls are not being called simultaneously because one request waits for the previous request, and then it starts, if all of you need some info, just leave it in the comments please, My head is burning out with this.


EDIT: Sorry for taking your time, it’s probably a PHP cURL problem, question closed.

It’s unclear from your question why your AJAX call is in an async function. $.ajax already returns a promise if you need it to.

I’ve used a fake API call here that mirrors your post AJAX call but returns values at random times so you can see how it works. The fake API function won’t be called simultaneously because it’s obviously being called in a loop, but the calls to the API don’t have to wait for each other either.

As you can see they all return at different times whenever the code is run.

const Agendamento = {
  storeResultados: function(consulta) {

    // Pretend this part is your AJAX call
    const rnd = Math.random() * ((5 - 1) + 1) * 1000;
    return new Promise(res => {
      setTimeout(() => res(consulta), rnd);
    });
  }
}

const consultas = [1, 2, 3];

// Here I'm just looping over your array and returning the AJAX
// promise, then I log the data
for (let consulta of consultas) {
  const promise = Agendamento.storeResultados(consulta);
  promise.then(data => {
    console.log(data);
  });
}