I'm new to this whole concept of asyncronous programming in JS, and I'm a little bit unsure why this is happening. This is my code
const fetch = require('node-fetch');
const apiSite = `https://api.github.com/users/`;
const getGithubUserObject = async (username) => {
let settings = {method: "GET"};
try {
const response = await fetch(apiSite + username, settings);
const responseJson = await response.json();
return await responseJson;
}
catch(e) {
console.log(`Error + ${e}`);
}
}
let myObj = getGithubUserObject("example");
console.log(myObj);
The problem I encountered is that myObj is a Promise, but in getGithubUserObject
I'm returning await responseJson
, and it's waiting for response
to be done. If instead of returning it I print it out it works fine. Is this an intended behaviour of async/await? How can I return the value without using .then()
or similars outside the function?