Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
Promise {<pending>} being returned and await key word not working

When running the below code and dumping out the results variable it returns Promise {<pending>}, I have added the await key word to the being of the function call so const results = await getAllResults() however this returns the error of Unexpected reserved word 'await'.

Anyone have any ideas?

useEffect(() => {

    async function getPageOfResults(page) {
        const response = await axios.get('https://swapi.dev/api/starships/?page=' + page);
        return response.data.results; 
    }

    async function getAllResults() {
        let starships = [];
        let lastResultsLength = 10;
        let page = 1;
        while (lastResultsLength === 10) {
            const newResults = await getPageOfResults(page);
            page++;
            lastResultsLength = newResults.length;
            starships = starships.concat(newResults);
        }

        return starships;
    }

    const results = getAllResults();

}, []);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to add async in the useEffect like so:

useEffect(async () => {

    async function getPageOfResults(page) {
        const response = await axios.get('https://swapi.dev/api/starships/?page=' + page);
        return response.data.results; 
    }

    async function getAllResults() {
        let starships = [];
        let lastResultsLength = 10;
        let page = 1;
        while (lastResultsLength === 10) {
            const newResults = await getPageOfResults(page);
            page++;
            lastResultsLength = newResults.length;
            starships = starships.concat(newResults);
        }

        return starships;
    }

    const results = await getAllResults();

}, []);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda