0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【TS】forEachの中でasync,awaitは使えない

Posted at

forEachの中でasync,awaitは使えない

forEachのコールバック関数はasync,awaitに対応していないので以下のエラーとなる

Object.values(validationErrors).forEach(val => {
  // 'await' 式は、非同期関数内と、モジュールのトップ レベルでのみ許可されます。
  await new Promise((resolve) => setTimeout(resolve, 500))
  console.error(val)
})

ループでasync,awaitを使いたいならconst x of Objectを使う

const x of Objectの中であれば、awaitだけで非同期処理を同期的に使える

for (const val of Object.values(validationErrors)) {
  await new Promise((resolve) => setTimeout(resolve, 1000));
  console.error(val)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?