I make several API calls, after which I want to load the combined results of each call:
$.when( $.get(localAPI, data, function(response) { globalStore.localShares = Number(response); }), $.get(facebookAPI, '', function(response){ globalStore.facebookShares = Number(response[0].share_count); }), $.getJSON(pinterestAPI, {url: url}).done(function(response){ globalStore.pinterestShares = Number(response.count); }) ).always(function(){
If the call to $.get fails, the callback function $.always is still executing.
But
If only one call to $.get fails, it cancels the previous calls.
So, if the first call fails, globalStore returns with two elements. If the first call succeeds but the second fails, globalStore returns only one item. And if the first two calls succeed, but the last fails, globalStore returns empty.
Is there any way around this?
Edit:
Yes, I tried to handle the crash in $.when as follows:
$.when( $.get(mu30_ajax_frontend.ajaxurl, data, function(response) { globalStore.localShares = Number(response); }).fail(function(){ globalStore.localShares = 0; }), $.get(facebookAPI, '', function(response){ globalStore.facebookShares = Number(response[0].share_count); }).fail(function(){ globalStore.facebookShares = 0; }), $.getJSON(pinterestAPI, {url: url}).done(function(response){ globalStore.pinterestShares = Number(response.count); }).fail(function(){ globalStore.pinterestShares = 0; }) ).always(function(){
But I get the same result.