Home > Blockchain >  Why won't a function inside a function give any output
Why won't a function inside a function give any output

Time:06-19

So here's my code:

const request = require('request');
url = 'https://en.wikipedia.org/api/rest_v1/page/random/summary';
title = null;
module.exports.title = title;

function myFunc() {
request(url, (error, response, body)=>{
            console.log(body);
            dataObj = JSON.parse(body);
            console.log(dataObj);
            console.log(dataObj.title);
            console.log(dataObj.extract);
            title = dataObj.title;
});
}

When I run the code the console gives me no output. I'm simply curious why putting my request function inside another function results in zero output.

CodePudding user response:

function myFunc() {
request(url, (error, response, body)=>{
            console.log(body);
            dataObj = JSON.parse(body);
            console.log(dataObj);
            console.log(dataObj.title);
            console.log(dataObj.extract);
            title = dataObj.title;
});
}

Add this bellow your code, let's see how it's work, hope you can learn something here about console.log it's very important.

console.log(myFunc)
console.log(myFunc())

CodePudding user response:

return the function request(...) and call the function myFunc(), otherwise, you would never get an output.

  • Related