Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,120 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi
I have the following code in javascript:
var dadosJSON = '';
async function lerArquivoJSON(caminhoDoArquivo) {
const resposta = await fetch(caminhoDoArquivo);
const dados = await resposta.json();
return dados;
}
async function main() {
dadosJSON = await lerArquivoJSON('dados1.json');
console.log(dadosJSON[4].time, dadosJSON[4].arquivo, dadosJSON[4].pais);
}
main();
console.log(dadosJSON[4].time, dadosJSON[4].arquivo, dadosJSON[4].pais);
Inside the main() function I can show the data in console.log. Why can't I access the data outside of the function?
Tks.
Try using then:
. . .
main().then( _ =>
{
console.log( dadosJSON[4].time, dadosJSON[4].arquivo, dadosJSON[4].pais );
} );