Run array with WHEN method, javascript

Asked

Viewed 602 times

0

Well, I have an array of numbers and I need to send each separate checker to the server, but I need to wait for all of them to be executed before I perform my final action. I searched and then discovered the method/function WHEN waiting for all requests to be closed to execute X function.

The problem is that I don’t know how to run all the contents of the array and insert it into that WHEN.. I have no idea at all..

See a sample of her:

$.when(ajax1(), ajax2(), ajax3(), ajax4()).done(function(a1, a2, a3, a4){

but my array data doesn’t have only a1, a2, a3 e a4 they can have much more up to "infinite" or simply 1 or 2 items.

My array in case: [1,2,3,4,5,6,6,77,44,23423,234212344]

Ah, I use it forEach to run the array

  • For example $.when(p1, p2).done(retorno1, retorno2){ alert(retorno1 + ' : ' + retorno2) } we have 2 requests that are p1 and p2, where your returns are retorno1 and retorno2, but still your doubt is unclear, and I don’t know exactly, what to answer.

  • And do you have all of these going from the array at once in an array or are you getting ids one by one? If you explain better how the application works becomes clearer

1 answer

1

I would say that sending "1 to infinite" ajax requests simultaneously is not a very good idea.

In fact jquery has a new API for files and you can pass several ajax requests as arguments for $.when() and then run a function when all have been run using the .done().

If you have a reasonable number in this array I think it’s a good idea to use the $.when(), or else I think you should use the .then(), for the requests to wait for each other.

To answer your question you can ask like this:

var array = [1,2,3,4,5,6,6,77,44,23423,234212344];
var ajaxArray= array.map(function(nr){
    return $.ajax({url: 'meusite.com', data: nr});
});
$.when.apply($, array).done(function(){
    alert('está pronto!');
    console.log(arguments); // array onde cada elemento corresponde à resposta de cada pedido ajax
});

If you want to chain instead of sending them all at once you can use it like this:

var pr = $.Deferred(); // promise vazia
array.forEach(function(el){
    pr = pr.then(function(){
        return $.ajax({url: 'meusite.com', data: nr});
    });
});

pr.then(function(){
    // esta é a ultima chamada e onde podes correr o codigo final
});
  • Hi, I said infinities just to try to make the question clearer, but the maximum would be 100 items in the array, would that be a lot? You have a better alternative?

  • @Elaine requests need to wait for each other? Can’t send all data in one request?

  • They do.. Unfortunately, see, each number corresponds to an ID that will search for its respective public key on the server to encrypt content.. and only after each has its own encrypted content that I can close with the final function. But although I could rescue all the keys at once.. has ideas?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.