I'm trying to get better at NodeJS, but I'm kinda confused about how to return functions properly. That's what I have:
var express = require('express');
const fetch = require('node-fetch');
const url = 'https://api.github.com/users/github';
function getjsonname(link) {
fetch(link)
.then(res => res.json())
.then(json => {
console.log(json.name + ' log inside function works'); // It returns "github log inside function works" properly
return json.name; // This is how I'm trying to return, which should work like the log above
}).catch(err => console.error(err));
}
console.log(getjsonname(url)); // It only returns "undefined"
Mostly topics say it's because async, callback or I\O, but I don't understand why it works in the log inside and not in the outside. The concept is kinda confusing for me yet.