Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!