Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8697

firebase の onAuthStateChanged 同期処理

$
0
0

概要

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

Viewing all articles
Browse latest Browse all 8697

Trending Articles