LoginSignup
17
7

More than 3 years have passed since last update.

firebase の onAuthStateChanged 同期処理

Posted at

概要

firebaseのonAuthStateChangedが非同期処理だったのでpromiseで同期処理にした

コード

function auth() {
return new Promise(resolve => {
    firebase.auth().onAuthStateChanged(user => {
      console.log('1')
      // userを使用した処理とかかく
      resolve()
    })
  })
}

async function sample() {
  await auth()
  console.log('2')
}
結果
> 1
> 2

以下の書き方だと同期処理にならない

async function sample() {
  await firebase.auth().onAuthStateChanged(user => {
    console.log('1')
  })
  console.log('2')
}
結果
> 2
> 1
17
7
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
17
7