Async - collecting two results
example code for http://stackoverflow.com/questions/9266841/variables-inside-functions-inside-functions-in-javascript
by bjelline
HTML
<input id="start" value="start" type="button">
<ol id="output"><li>what happens when?</li></ol>
JavaScript
var url1 = "/"; // the first api you want to call
var url2 = "/"; // the second api you want to call
function out(s) {
$('#output').append('<li>' + s + '</li>');
}
function grabdata() {
$.when($.ajax(url1), $.ajax(url2)).then(
function(d1, d2) {
out('both calles returned');
out('call 1 returned ' + d1[0].length + ' bytes of data');
out('call 2 returned ' + d2[0].length + ' bytes of data');
// here is where you do more stuff with the data
}, function() {
out('one call returned');
});
out('at the end of grabdata().');
// nothing intresing to do, nothing to return
}
$('#start').click(grabdata);