2

I am new to Nodejs, and am still learning how to work around it's asynchronous ways. There are many other answers that cover using results of a callback response from an http get request in node, however, I do not want to use the results right away, but rather return the results to a different function, and continue one I have received all responses.

So I have a file like http-request.js

var conf = require('./config.js');
var https = require("https");

exports.httpRequest = function (param1, param2) {
var url = "https://some.restapi.com/api/search/json?param1="+param1+"&param2="+param2;
https.get(url, function(response) { 
    response.on('data', function (chunk) {
        body += chunk;
    });
    response.on('end', function () {
        json = JSON.parse(body);
        for(var name in json.results){
            val1 = json.results[name].property.prop.value1;
            val2 = json.results[name].property.prop.value2;
            var vals = val1 + "," + val2;
            arr.push(vals);
        }
        //This would be the place I find I am suppose to use the result.
        //But I cannot do this, instead, I need to return the array to another function
        return arr;
    });
  });
}

And the main file called Demo.js

var arr1 = [];
var arr2 = [];
var arr3 = [];
var httpreq = require('./http-request.js');

arr1 = httpreq.httpRequest(param, key1);
arr2 = httpreq.httpRequest(param, key2);
arr3 = httpreq.httpRequest(param, key3);

My problem is that arr1 - arr3 are undefined, because they are assigned values before the response is received. How can I wait for all the responses from a different file before I attempt to use any values?

Cole9350
  • 5,444
  • 2
  • 34
  • 50
  • You can't wait and you can't return. You need to instead pass a function as a callback so that you may have access to the result. Or you can pass a function as a callback to a promise you've returned. – slebetman Oct 20 '14 at 19:42
  • It's an asynchronous task, the results don't exist at the moment the `arr*`-variables are declared. Thus there's no return-value for your `arr*`-variable. You rather have to pass a callback-function into the `httpRequest`-method and assign the variable inside of it. – Emanuel Kluge Oct 20 '14 at 19:43
  • 1
    You should probably read this: [How to return the response from an Ajax call](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call). It's about ajax calls in the browser, but the async issue is identical. Summary: you can't return from your function with the results of an async operation. Your function will return and finish long, long before the async operation has completed. You have to write a different type of async-compatible code using promises or callbacks. If you're developing in node, you must understand how to do this. – jfriend00 Oct 20 '14 at 19:48
  • @jfriend thanks for that link, it was very helpful. I am trying to think of ways to restructure the code, but I just don't see that it's possible. In this case, I need to wait for all the searches to return before I can do anything with it. Should I not be using nodejs? – Cole9350 Oct 21 '14 at 19:34
  • If you need to wait for multiple asynchronous activities to be done, then you should probably use a library that helps manage asynchronous things such as Bluebird's promise library or the Async library. That's the common best practice for managing multiple asynchronous activities. Node is perfectly fine as a tool (even better when doing lots of asynchronous things). You just have to learn how to program in Javascript with asynchronous activities. If you commit to learning it, it isn't that hard, but it is something to commit to learning. – jfriend00 Oct 21 '14 at 19:38

0 Answers0