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?

async/await 自分用メモ

Last updated at Posted at 2025-07-17

データを受け取ってから次の処理をしたいときにasync/awaitを使う

async function fetchUserData() {
  // ここで「データが返ってくるまで待つ」
  const res = await fetch('/api/user');
  const data = await res.json();

  // データを受け取ってから次の処理へ
  console.log('ユーザー名:', data.name);
  // ここで受け取ったデータを使って何かする(画面表示とか)
}

この場合、

  • fetch()が終わってレスポンスを受け取るまで待つ

  • res.json()でデータをJSONに変換するのを待つ

  • そのあとでやっとconsole.logなどの次の処理が走る


まとめると

  • fetch()res.json() はどちらもPromiseを返す非同期処理

  • Promiseは「結果がまだ返ってきていないけど、後で返す約束の箱」のようなもの

  • だから await を使って、そのPromiseが終わるまで待つ必要がある

  • async は、その関数の中で await を使うために必要な合図


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?