LoginSignup
2
6

More than 3 years have passed since last update.

async/awaitで非同期処理の完了を待つ

Posted at

①非同期処理をおこなう関数を宣言


const 関数名 = async () => {
  const message = 'GitのユーザーIDは';
  const url = 'https://api.github.com/users/taizo-pro'

②awaitをつけ、fetchが実行完了するまで次に進まなくさせる

// jsonにreturnされた値が入る    
const json = await fetch(url)
      .then(res => {
          console.log('非同期処理成功時のメッセージです')
          return res.json()
      }).catch(error => {
          console.error('非同期処理失敗時のメッセージです。', error)
          return null
      });

const username = json.login;
  console.log(message + username)
}
関数名()
2
6
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
2
6