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?

More than 1 year has passed since last update.

非同期処理

Last updated at Posted at 2023-01-11

非同期処理とは

前の処理が終わっていない状態でも後ろの処理を行うこと。

async awaitでの記述方法とPromiseでの記述方法(同義)

// 5秒待つ関数
const wait5Seconds = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log("finish!");
      resolve({result: "ok"});
    }, 5000)
  })
};
// Promise版
const onClickBtn = () => {
  wait5Seconds() // Promise
    .then((response) => {
      console.log("wait finish!");
      console.log(response.result); // "ok"
    });
};
// async/await版
const onClickBtn2 = async () => {
  const response = await wait5Seconds();
  console.log("wait finish!");
  console.log(response.result); // "ok"
};
0
0
1

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?