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 await inside a if condition has no effect in my code?

I’m coding in react and I realized that my await nas no effect (at least that’s what my vscode is alerting to me)


const voltar = async (argumento) => {
        if(argumento == "autoriza"){
            await autoriza()
        }
        fc_volta();
        history('/')
    }

    const autoriza = () => {
        fetch('https://api.github.com/users/someuser/repos')
        .then(resposta => resposta.json())
        .then(dados => console.log(dados)) 
    }

Can someone explain to me what is wrong?

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 :

Here is how you would rewrite it, using await:

const voltar = async argumento => {
  if(argumento === "autoriza"){
    await autoriza();
  }
  fc_volta();
  history('/');
}

const autoriza = async () => {
  let resposta = await fetch('https://api.github.com/users/someuser/repos');
  let dados = await resposta.json();
  console.log(dados);
  return dados;
}
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