Next.js Discord

Discord Forum

how to use async

Unanswered
Polar bear posted this in #help-forum
Open in Discord
Polar bearOP
I am trying to use async keyword in Array.map in next.js. But every time I use this, I get an error saying Application error: a client-side exception has occurred (see the browser console for more information). How do I solve this?

code: https://github.com/chat-ana/ana-chat/blob/main/src/pages/index.js#L103

3 Replies

You can’t directly await async functions in map, specifically you are not on App Router. Generally, we will use Promise.all to convert an array of promises to result.

However, you cannot await a promise in React.js components, please consider putting it in a useEffect, and store the result to a state.

useEffect(() => {
  const execute = async () => {
    …
  }
  execute().then(res => setState(res))
}), []);
Polar bearOP
thx!
I'll try