javascript multiple ajax calls, single callback? -



javascript multiple ajax calls, single callback? -

my first post here, hi :)

i have js code access json api:

function a() { //does things //... //then calls function b b(some_params); } function b(params) { //calls objects method makes ajax phone call api , response in json someobject.makeajaxcalltoapi(params, function(response) { alertfunction(response); }); } function alertfunction(resp) { console.log ("the response is: "); console.log(resp); }

this working ok, need modify in order this: in function a(), instead of making single phone call b(), need phone call b() multiple times in loop, different parameters each time. , want phone call alertfunction() passing array responses, after responses have been received.

i have tried utilize $.when , .then, after seeing examples on deferred objects, not working:

function a() { //does things //... //then calls function b var allresponses = []; $.when( anarray.foreach(function(element) { allresponses.push(b(some_params)); }); ).then(function() { alertfunction(allresponses); }); } function b(params) { //calls objects method makes ajax phone call api , response in json someobject.makeajaxcalltoapi(params, function(response) { //alertfunction(response); }); homecoming response; } function alertfunction(allresp) { console.log ("the responses are: "); console.log(allresp); }

any help?

update - ok got working. set here final code in case helps else...

function a() { //does things //... //then calls function b var requests = []; //-- populate requests array anarray.foreach(function(element) { requests.push(b(some_params)); }); $.when.apply($, requests).then(function() { alertfunction(arguments); }); } function b(params) { var def = $.deferred(); //calls objects method makes ajax phone call api , response in json someobject.makeajaxcalltoapi(params, function(response) { def.resolve(response); }); homecoming def.promise(); } function alertfunction(allresp) { console.log ("the responses are: "); console.log(allresp); }

here 1 way utilize $.when unknown number of ajax calls:

$(function () { var requests = []; //-- loop generate requests (var = 0; < 5; i++) { requests.push( $.getjson('...') ); } //-- apply array $.when() $.when.apply($, requests).then(function () { //-- arguments contain results console.log(arguments); }); }); edit

applied code, should this:

function a() { var requests = []; //-- populate requests array anarray.foreach(function(element) { requests.push(b(some_params)); }); $.when.apply($, requests).then(function() { alertfunction(arguments); }); } function b(params) { //-- in order work, must phone call asynchronous //-- jquery function without providing callback homecoming someobject.makeajaxcalltoapi(); } function alertfunction(allresp) { console.log ("the responses are: "); console.log(allresp); }

javascript callback

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -