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

117
Views
async function inside new async function

I need to fetch data from Mongo db using mongoose then send some request using node-fetch. So I created below async function

async function mainThree() {

    await ProvinceList.find({}).then((data) => {

        data.forEach((provience) => {
            console.log(provience.id)
            let response = await fetch(`https://xxxx.com/api/getSubAddressList?addressId=${provience.id}`, {
                "headers": {
                    "accept": "application/json, text/plain, */*",
                    "accept-language": "en-US,en;q=0.9,si;q=0.8",
                    "cache-control": "no-cache",

                },
                "referrer": "https://www.xxxx.com/",
                "referrerPolicy": "strict-origin-when-cross-origin",
                "body": null,
                "method": "GET",
                "mode": "cors"
            });

            let datalist = await response.json();


            CityList.insertMany(datalist.module);

        });


    });



}

but when I running code I am getting

**SyntaxError: await is only valid in async function**

I need to know How I run Async function insite Async function

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In this case, you're using the await inside of the function passed to forEach. You need to make that function async.

data.forEach(async (province) => ...

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!