Last active
August 29, 2015 14:05
-
-
Save idooo/0a3e562d1df4b019dd09 to your computer and use it in GitHub Desktop.
Question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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