LoginSignup
7
3

More than 3 years have passed since last update.

アロー関数でasync/awaitしてみた備忘録(JavaScript)

Last updated at Posted at 2019-07-12

Qiitaで記事読んでてawaitいいじゃん使ってみよってなって書いたのが以下。例えばFirestoreからデータを取って来るコード。async/awaitをアロー関数で即時実行するように使うとこんな感じ。

(async () => {
  const querySnapshot = await db.collection('users').get()
  if (!querySnapshot.docs.length) {
    throw new Error('Empty!')
  }
  console.log(querySnapshot.docs[0].data())
})().catch(error => console.error(error))

このコードだと何も恩恵はないけど、非同期処理でありがちなpromise.thenがネストしちゃう時は効果バツグンだと思います。

7
3
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
7
3