-1

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?

  • 3
    `autoriza` isn't `async` or doesn't return a Promise. – kelsny Apr 14 '23 at 19:20
  • 1
    Depends entirely on what `authoriza()` does. If it's not async code which produces a promise, then `await()` doesn't do anything useful - true. If it is async code which produces a promise and the promise resolves when the async code finishes - then `await` is definitely useful. – VLAZ Apr 14 '23 at 19:21
  • 1
    To give a real answer, it will be necessary to see what "some fetch code" actually means. – Pointy Apr 14 '23 at 19:22
  • 2
    you need to return the fetch – Sysix Apr 14 '23 at 19:27

1 Answers1

0

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;
}
Andrew Parks
  • 6,358
  • 2
  • 12
  • 27