Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why won't a function inside a function give any output

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading