0

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;
    });

}
fraxture
  • 5,113
  • 4
  • 43
  • 83
  • See this here: http://stackoverflow.com/questions/16866015/node-js-wait-for-callback-of-rest-service-that-makes-http-request – Fabian Lurz May 02 '15 at 11:33
  • The canonical answer to your question is the marked duplicate. @Fabian has linked to node-specific ways of handling the problem. (BTW, you don't really need the `async` library, you are handling merely one single call to the API) – Tomalak May 02 '15 at 11:43
  • The answer that @Fabian linked here does not I don't think help me. The problem that I'm having, which I think I may have not clarified well enough, is that when I call the above function, it returns "undefined". That is the output is "undefined" followed by the correct output of `console.log(result)`. – fraxture May 02 '15 at 11:58
  • Sorry I didn't realize that the duplicate is at the top. I understand the problem outlined their, but the solutions proposed seem to be mostly tailored for AJAX, and not Node. I guess I was looking for best practice solution in this case for Node and Node's http.request. – fraxture May 02 '15 at 12:05
  • @fraxture: The situation is the same regardless if it's AJAX, web workers, setTimeout or http.request -- you cannot return form asynchronous functions, you have to pass it a callback to execute when it completes. Your `getFlickrSizes` function needs to accept a callback and you should call it the way you called `http.request` – slebetman May 02 '15 at 13:11
  • @fraxture: See this answer, maybe the explanation is clearer: http://stackoverflow.com/questions/17460556/undefined-return-value-from-the-function-call-javascritpt/17460802#17460802 – slebetman May 02 '15 at 13:16
  • @slebetman thanks for this other link. it is helpful to some extent as well. i like hot it ends by showing what async code looks like, i.e. with a great deal of nesting! – fraxture May 02 '15 at 13:45
  • Yes, a lot of answers to these sort of questions are technically correct but often fail to show how the code structure should look like. If you want to avoid nesting take a look at promises – slebetman May 02 '15 at 13:50

0 Answers0