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を繰り返し呼び出して制御する

Posted at

Promiseを繰り返し呼び出して制御する

Promise を繰り返し呼び出しなんらかのフラグにより呼び出しを中止します

var loopCounter = 5;
function LoopPromise() {
    console.log("LoopPromise: ", loopCounter);
    if(loopCounter > 0) {
        new Promise(function(resolve) {
            setTimeout(function() {
                loopCounter -= 1;
                LoopPromise();
                resolve(true);
            }, 1000);
        });
    } else {
        // NOP
    }
}

想定される使い道

下記のような使い方を想定しています

  • APIのポーリング … レスポンスが0の間は呼び出し続ける、1になったら次の処理へ遷移する
  • 指定件数読み込むまで繰り返す … 一度に読めるデータが100件のAPIで1000件読み込むまで繰り返す
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?