loadingCtrl dismiss if the data is empty

I am trying to dimiss from th loadingCtrl when the data is empty

This is my example:

constructor(
private loadingCtrl: LoadingController,
) {

this.recapServiceProvider.getRecap(this.loginServiceProvider.getToken())
  .subscribe(
  data => {        
    loading.dismiss();        
  });
 let loading = this.presentLoadingDefault();

}

When the data is empty, the loading still running.

How can I dismiss the loading when the data is empty

I mean…you’re defining your loading variable after you use it. Granted since it’s an async call…you’re technically calling it after, still, define the variable first.

Also, the code you posted is incomplete, we don’t know what this.presentLoadingDefault() does.

this is the presentLoadingDefault()

presentLoadingDefault() {
let loading = this.loadingCtrl.create({
content: ‘Chargement des données…’
});
loading.present();
return loading;
}

Gotcha, that seems reasonable. Try refactoring your code so you declare the loading variable first.

const loading = this.presentLoadingDefault();
this.recapServiceProvider.getRecap(this.loginServiceProvider.getToken())
  .subscribe(data => {
    loading.dismiss();
    console.log('My callback ran!');
  });

I also just did a minor cleanup/simplification of the code. This should work I think. I added a simple log so you could make sure your subscribe callback is running. If you don’t see that logged to your console, you’ve got something wrong there. Not sure what getRecap and getToken return either, so hard to say. But if nothing is logged to the console, you have an issue there.

It’s work. thank you for your reply