Amaury Laroze
Amaury Laroze

Reputation: 217

How to use the Loading Controller with duration

I have my LoadingController :

const loader = this.loadCtrl.create({
     content: 'Loading ...',
     duration: 5000
});
loader.present();

Just after i have an api call

myApi.Function().then(res => {
   console.log(res);
}

I want to dismiss my LoadingController when my api call is end OR if my duration is end.

How can i call the event loader.dismiss() after the duration ?

thanks,

Upvotes: 0

Views: 548

Answers (1)

Thomas
Thomas

Reputation: 8849

You can use Promise.race() for that:

The Promise.race() method returns a promise that resolves or rejects as soon as one of the promises in an iterable resolves or rejects, with the value or reason from that promise.

const delayPromise = new Promise(resolve => window.setTimeout(() => resolve(), 3000));
const apiPromise = myApi.Function();

const loader = this.loadCtrl.create({
     content: 'Loading ...',
});
loader.present();

Promise.race([delayPromise, apiPromise]).then(res => {
    loader.dismiss();

    if(res) {
        // apiPromise finished first
    } else {
        // delayPromise finished first
    }
});

Upvotes: 1

Related Questions