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?

Promiseについて

Last updated at Posted at 2024-09-08

promiseについて勉強したのでメモ

promiseのインスタンス化

new Promise((resolve,reject)=>{
    処理
})

resolveとrejectはどこから受け取るのか?

JavaScriptエンジンが自動的に提供する。これらの引数は実行関数である。

.then resolveしたときに呼び出される

.catch rejectされたときに呼び出される

const myPromise = new Promise((resolve, reject) => {
    const success = true; // 成功した場合

    if (success) {
        resolve('成功しました!'); // ここで呼び出されるのはJavaScriptエンジンが提供したresolve関数
    } else {
        reject('失敗しました!'); // ここで呼び出されるのはJavaScriptエンジンが提供したreject関数
    }
});

// 使用例
myPromise
    .then((message) => {
        console.log(message); // "成功しました!" が表示される
    })
    .catch((error) => {
        console.error(error); // エラーが発生した場合に呼び出される
    });

上の場合const success = true;としてるので、resolveの方が呼び出される。

0
0
2

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?