LoginSignup
0
0

More than 3 years have passed since last update.

[js] 指定したPromiseが解決されるまでローディング表示

Posted at

概要

a.gif

Promiseを渡すとそれが解決されるまでローディング表示をする関数を実装する。

ローディング関数の仕組み

  1. 最初にローディング表示する
  2. 渡されたPromiseが解決されたらローディング表示を解除する

Promiseの解決を検知する方法

渡されたPromiseに対してthenメソッドを呼び出すことでPromiseが解決されたことを検知できる。

promise.then(() => console.log('解決された'))

実装

function loading(promise) {
  let i = 0;
  const interval = setInterval(() => {
    showLoading(((++i) % 3) + 1);
  }, 500);
  promise.then(() => {
    clearInterval(interval);
    endLoading();
  });
}

デモ

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