Skip to content

Instantly share code, notes, and snippets.

@idooo
Last active August 29, 2015 14:05
Show Gist options
  • Save idooo/0a3e562d1df4b019dd09 to your computer and use it in GitHub Desktop.
Save idooo/0a3e562d1df4b019dd09 to your computer and use it in GitHub Desktop.
Question
// You have an array with links to your backend server
var links = ['http://server.com/link1', 'http://server.com/link2', ...];
// Also you have a simple async function that execute ajax request
// and then execute a callback passing server response as an argument
sendRequest(link, callback(response));
// Write a function that will get data from all the links
// and then print ONE ARRAY with ALL the server responses to browser's console
function printArray(...) {
...
}
// Answer
// ---------------------------------------------------------------------------------
function printArray(links, callback, result) {
if (typeof result === 'undefined') result = [];
if (links.length) {
sendRequest(links.shift(), function(response) {
result.push(response);
printArray(links, callback, result)
});
}
else {
callback(result);
}
}
printArray(links, function(array) {
console.log(array)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment