1

I think I'm fundamentally misunderstanding something here in Javascript.

Could someone please explain why I get SyntaxError: Unexpected identifier when trying to call getUrlContent from inside the forEach:

(async () => {

    let sitesState = []
    urlsToCheck.forEach(url => {
        sitesState.push({
            "url": url,
            "data": await getUrlContent(url)
        })
    })

})();

async function getUrlContent(url) { ... }

Why does this happen (and of course, how do I write this correctly?).

Any help appreciated.

Thanks.

userMod2
  • 8,312
  • 13
  • 63
  • 115

1 Answers1

1

You've used async in the wrong function. Check this.

    ( () => {

    let sitesState = []
    urlsToCheck.forEach(async(url) => {
        sitesState.push({
            "url": url,
            "data": await getUrlContent(url)
        })
    })

})();

async function getUrlContent(url) { ... }
Rahul Purohit
  • 540
  • 4
  • 16