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 5 years have passed since last update.

JS Promise

Posted at

Promise

Promiseオブジェクトは非同期処理の最終的な完了処理(もしくは失敗)及びその結果の値を表現します。

var promise1 = new Promise(function(resolve, reject){
      setTimeout(function(){
        resolve('foo'); 
      }, 300);
    });

    promise1.then(function(value){
      console.log(value);
    });

    console.log(promise1);

    //Promise {<pending>}
    //"foo"

構文

new Promise(executor);

引数

  • executor
  • 二つの引数resolveとrejectを取る関数。executor関数はresolve関数とreject関数が渡されてPromiseが実装されるとすぐに実行されます(Promiseコンストラクターがオブジェクトを返すよりも前にexecutorは実行されます)。resolve関数とreject関数は呼び出されるとPromiseに対してそれぞれresolve(解決)もしくはreject(拒否)を行います。executorは通常、非同期の作業を開始して、完了した際にresolve関数(成功した場合)もしくはreject関数(エラーが発生した場合)のいずれか一方を呼び出します。executor関数でエラーが投げられた場合、Promiseはrejectされます。executorの返り血は無視されます。

参考

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?