I have some code that makes a request to an API (Flickr) in an attempt to get some data. The call is abstracted in a function so that I can call it elsewhere. The trouble I am having -- which I think is based in lack of experience in dealing with async in Node -- is that I can't figure out how to wait for the async call to finish before returning from the function. What is the right way to do this? Here is what the function looks like:
var https = require('https'),
async = require('async');
function getFlickrSizes(link) {
var id = parseFlickrPhotoId(link),
options;
options = {
host: 'api.flickr.com',
path: '/services/rest/?method=flickr.photos.getSizes&&api_key=b2fbcde54379c607e0516aff52dd3839&photo_id='+id+'&format=json&nojsoncallback=1&api_sig=d16f5207517655e640e23c9f458610a6',
};
async.parallel([
function(callback) {
https.request(options, function(resp) {
var str = '';
resp.on('data', function(data) {
str += data;
});
resp.on('end', function() {
callback(false, JSON.parse(str).sizes);
});
}).end();
}
], function(err, result) {
console.log(result);
return result.sizes;
});
}