javascript - how to execute multiple callbacks and return results array? -


i trying array of image urls inside dropbox in json format. in order that, have written following function:

router.get('/thumbnails', function(req, res, next){ var images = []; var imagescount = -1; var isloggedin = !!req.user; var dropboxclient = req.app.get('dropboxclient'); console.log(dropboxclient); dropboxclient.authenticate({interactive: false}, function(error, client) {     if (error) {         console.log(error);         return 0     }     if (dropboxclient.isauthenticated()) {         dropboxclient.readdir(dropboxconfig.folder, function(error, entries) {             if (error) {                 console.log(error);                 return 0             }             imagescount = entries.length;             entries.foreach(function(entry){                 var path = dropboxconfig.folder + '/' + entry;                 dropboxclient.makeurl(path, {download: true}, function(err, res){                     if(err){                         console.log(err);                         return 0                     }                     //console.log(res.url);                     images.push({url: res.url});                     console.log("processed " + images.length + "/" + imagescount);                     if(images.length == imagescount){                         console.log(images);                         res.json(images);                     }                 });              });         });     }  }); 

});

upon observing console output believe urls loaded array, data not available on address http://.../thumbnails , after while, if trying load data in browser, returns err_empty_response.

does know why function doesn't return data?

dropboxclient.makeurl(path, {download: true}, function(err, dbc_res){     if(err){         console.log(err);         return 0     }      //console.log(res.url);     images.push({url: dbc_res.url});     console.log("processed " + images.length + "/" + imagescount);     if(images.length == imagescount){         console.log(images);         //res come dropboxclient, not router.get!!!         res.json(images);     } }); 

the problem @ res.json, answer dropboxclient, not user, need rename res @ dropboxclient.makeurl(path, {download: true}, function(err, res){, user answer.


Comments