0

I am trying to create functions file for my project with node js (v6.14.3).

From test file I am using 'require' to call my helper.js file (the function file), and access the gethttp function I created. First I tried to use async/await and then realize it used in older versions.

I saw I need to use promise/callback and didn't understand how.

helper.js :

const http = require('http');
module.exports.gethttp = function(url){
    http.get(url, (resp) =>{
        let data = '';
         resp.on('data', (chunk) =>{
             data += chunk;
         });
         resp.on('end', (data) =>{
             return data;
         });
    }).on('error',  (err) =>{
        console.log(err);
    });
}

test.js :

var helper = require('./helper');
var req = helper.gethttp("the url");
console.log(req);

and getting the output of undefined...

Fusu
  • 17
  • 4
  • Why in `gethttp` do you return your value inside a callback? The callback is another scope. You can't return in function's output a result returned inside the function by asynchronos operation. You better return a promise. – 0xC0DEGURU Aug 18 '19 at 06:48
  • For returning data from an asynchronous operation, see [How do I return data from an asynchronous call](https://stackoverflow.com/a/14220323/816620). Probably you want to use the `request-promise` module since it already collects the http response for you and returns a promise which allows you return that from your function and the caller users the promise to get the data. You CAN'T return the data from your function. Your function returns long before the data is done arriving. – jfriend00 Aug 18 '19 at 07:25

2 Answers2

0

You can use the request promise module which internally use the http module, and return a promise, so we can use async await here.

const rp = require('request-promise');
module.exports.gethttp = async (url) => {
    const response = await rp(url);
    return response;
}

Wrote a self executing function, as await can be only used in a async function.

var helper = require('./helper');
(async () => {
var res = await helper.gethttp("the url");
console.log(res);
})();
Nayan Patel
  • 1,683
  • 25
  • 27
  • I understood that async cannot use is this version of node. I tried your code and got 'Unexpected token'. – Fusu Aug 18 '19 at 06:55
0

I haven't tested this code but that is the idea.

module.exports.gethttp = function(url){
    return new Promise((resolve, reject) => {
        http.get(url, (resp) =>{
            let data = '';
             resp.on('data', (chunk) =>{
                 data += chunk;
             });
             resp.on('end', (data) =>{
                 resolve(data);
             });
        }).on('error',  reject);
    });
}

And call your function like so

var helper = require('./helper');
var req = helper.gethttp("the url").then((data)=>{
    console.log(data);
}).catch((err)=>{
    console.error(err);
})

Or just

var helper = require('./helper');
var req = await helper.gethttp("the url");
Dan D.
  • 73,243
  • 15
  • 104
  • 123
0xC0DEGURU
  • 1,432
  • 1
  • 18
  • 39