score:1

Accepted answer

You could expand your call back inside here:

dataSources.forEach(function (source, index) {
  q.defer(function (callback) {
    d3.csv(source, function(data){
       var fileData = {
           name: dataSources[index],
           data: data
       };
       callback(fileData);
   });
  });
});

Based on your comment you could try doing the following but not 100% sure that it will work :

 var finalData = {};

 dataSources.forEach(function (source, index) {
  q.defer(function (callback) {
    d3.csv(source, function(data){
       if (data) {
         //regex the key from file name
         var matcher = /^(myData)_([a-z][a-z][a-z][0-9])\.csv$/;
         var key = matcher.exec(dataSources[index])[1];
         //assign value to external object
         finalData[key] = data;
         //callback success
         callback(true);
       }
       else {
        //log error if required
        callback(false);
       }
   });
  });
});

// Wait for all requests to be completed:
q.awaitAll(function (error, results) {
   console.log(finalData);
})

Hope this solves your issue.


Related Query

More Query from same tag