Так? Или что там надо...
function getData(url, callback) {
var RESPONSES = {
'/countries': [
{name: 'Cameroon', continent: 'Africa'},
{name :'Fiji Islands', continent: 'Oceania'},
{name: 'Guatemala', continent: 'North America'},
{name: 'Japan', continent: 'Asia'},
{name: 'Yugoslavia', continent: 'Europe'},
{name: 'Tanzania', continent: 'Africa'}
],
'/cities': [
{name: 'Bamenda', country: 'Cameroon'},
{name: 'Suva', country: 'Fiji Islands'},
{name: 'Quetzaltenango', country: 'Guatemala'},
{name: 'Osaka', country: 'Japan'},
{name: 'Subotica', country: 'Yugoslavia'},
{name: 'Zanzibar', country: 'Tanzania'},
],
'/populations': [
{count: 138000, name: 'Bamenda'},
{count: 77366, name: 'Suva'},
{count: 90801, name: 'Quetzaltenango'},
{count: 2595674, name: 'Osaka'},
{count: 100386, name: 'Subotica'},
{count: 157634, name: 'Zanzibar'}
]
};
setTimeout(function () {
var result = RESPONSES[url];
if (!result) {
return callback('Unknown url');
}
callback(null, result);
}, Math.round(Math.random * 1000));
}
/**
* Ваши изменения ниже
*/
var requests = ['/countries', '/cities', '/populations'];
var responses = {}; //, counter=0;
for (var i = 0; i < 3; i++) {
var _request = requests[i];
var callback = function(request){
return function (error, result) {
responses[request] = result;
var i, j;
if (Object.keys(responses).length == 3) { //or counter++==3
var c = [], cc = [], p = 0;
for (i = 0; i < responses['/countries'].length; i++) {
if (responses['/countries'][i].continent === 'Africa') {
c.push(responses['/countries'][i].name);
}
}
for (i = 0; i < responses['/cities'].length; i++) {
for (j = 0; j < c.length; j++) {
if (responses['/cities'][i].country === c[j]) {
cc.push(responses['/cities'][i].name);
}
}
}
for (i = 0; i < responses['/populations'].length; i++) {
for (j = 0; j < cc.length; j++) {
if (responses['/populations'][i].name === cc[j]) {
p += responses['/populations'][i].count;
}
}
}
console.log('Total population in African cities: ' + p);
}
}
}(_request);
getData(_request, callback);
}
Вообще такие задачи красиво решаются с Promise.all - сам код подсчета не писал
var requests = ['/countries', '/cities', '/populations'];
var responses = {};
function load(request){
return new Promise(function(resolve,reject){
getData(request,function(error, result){
if(!error){
resolve(result);
}else{
reject (error);
}
})
});
}
function getPopulation(state){
return Promise.all(requests.map(function(request){
return load(request);
})).then(function(data){
//console.log(arguments);
var countries=data[0],
cities=data[1],
populations=data[2],
result;
console.log(countries,cities,populations);
//calc here for state
return result;
},function(error){
console.warn("Data error",error);
});
}
getPopulation("Africa").then(console.log);