# Ionic Storage as promise with providing initial data

**URL:** <https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655>\
**Category:** ionic-v3\
**Created:** [January 27, 2018, 11:56pm UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655 "2018-01-27T23:56:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coffeeee](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@coffeeee](https://forum.ionicframework.com/u/coffeeee)\
**Post date:** [January 27, 2018, 11:56pm UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655/1 "2018-01-27T23:56:57Z")

</div>

Hello,

so I would like to achieve the following within my Ionic Storage provider (Ionic v. 3):

- on app open, check if storage is available
- if it is not available, create storage with http request, if http request not possible (return null), then populate with new data on the fly
- if available, simply use existing one.

This is my solution (provider):

```auto
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';

@Injectable()
export class MovieProvider {

  movieCollection: any;
  
  private initData() {
    this.buildMovieItemsViaHttp().then(data => this.movieCollection = data);
    if (this.movieCollection != null)
    {
      this.storage.set('storageMovieCollection', this.movieCollection);
    } else {
      //this.storage.set('storageMovieCollection', bla...load some generated data on the fly);
    }
  }

  private buildMovieItemsViaHttp(){
    return new Promise(resolve => {
        this.http.get('assets/data/movieCollection.json') //will be replaced with actual server data
          .subscribe(res => {
            resolve(res);
            console.log(res)
          });
    });
  }

  getMovieItems() {
    this.storage.get('storageMovieCollection').then((data) => {
      return data;
    });
  }

  constructor(private http: HttpClient, private storage: Storage) {
    this.storage.get('storageMovieCollection').then((data) => {
      if(data != null)
      {
        this.initData();
      }
      else
      {
        //just use existing one...
      }
    });
  }

```

The console.log will return all the items. So this is working.

However, doing the following inside my init will always return undefined:  
this.buildMovieItemsViaHttp().then(data =\> this.movieCollection = data);

Apparently it is not able to fetch the data from the promise. I fear I am using this wrong.

Thanks in advance for your ideas and suggestions!

---

<div class="post-metadata">

**Author:** ![Judgewest2000](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/judgewest2000/32/113339_2.png) [@Judgewest2000](https://forum.ionicframework.com/u/Judgewest2000)\
**Post date:** [January 28, 2018, 3:56am UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655/2 "2018-01-28T03:56:13Z")

</div>

```auto
private async initData() {
   this.movieCollection = await this.buildMovieItemsViaHttp();
    if (this.movieCollection != null)
    {
      this.storage.set('storageMovieCollection', this.movieCollection);
    } else {
      //this.storage.set('storageMovieCollection', bla...load some generated data on the fly);
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Judgewest2000](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/judgewest2000/32/113339_2.png) [@Judgewest2000](https://forum.ionicframework.com/u/Judgewest2000)\
**Post date:** [January 28, 2018, 3:57am UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655/3 "2018-01-28T03:57:23Z")

</div>

Also do yourself a favour… don’t cast the type to `any`. Cast to an interface that represents the shape of the data.

---

<div class="post-metadata">

**Author:** ![coffeeee](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@coffeeee](https://forum.ionicframework.com/u/coffeeee)\
**Post date:** [January 28, 2018, 5:35am UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655/4 "2018-01-28T05:35:49Z")

</div>

thanks ALOT for that, really I have been sitting for hours by now and rewrote everything couple of times without finding a solution 😃

I have one more issue:

My mainpage would load all the items from the provider storage on startup, like this:

```auto
  constructor(private navCtrl: NavController,
              private navParams: NavParams,
              private movieItemProv: MovieNewsProvider) {
  }

  ionViewWillEnter() {
    this.movieItemProv.getMovieItems().then(data => this.movie = data);
  }
  
  ionViewDidLoad() {
    this.movieItemProv.getMovieItems().then(data => this.movie = data);
  }

```

and this is my getMovieItems provider function to return the storage items:

```auto
  getMovieItems() {
    return new Promise(resolve => {
      this.storage.get('storageMovieCollection').then((data) => {
        resolve(data);
      });
    })
  }

```

the mainpage would only populate however if I switch the tabs, apparently because the storage is not yet finished with populating the data. However, I thought the promise would actually fix that.

So my question is: how can I ensure that my provider would finish all the data preperation/loading before the actual app starts? Should I put the data loading logic inside the app.components.ts to have the data loaded on app start?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Judgewest2000](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/judgewest2000/32/113339_2.png) [@Judgewest2000](https://forum.ionicframework.com/u/Judgewest2000)\
**Post date:** [January 28, 2018, 3:33pm UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655/5 "2018-01-28T15:33:41Z")

</div>

Probably not a bad idea.  
Just as long as that process doesn’t take too long.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [January 28, 2018, 3:59pm UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655/6 "2018-01-28T15:59:27Z")

</div>

> [@coffeeee](#):
>
> how can I ensure that my provider would finish all the data preperation/loading before the actual app starts?

You probably don’t want to lock yourself into this. User A has a great experience, while User B has a terrible experience, based on how fast their phone handles the loading. Instead, you can start the process of loading data in app.component.ts, but build your homepage so it is ready to display something cool even if not everything is loaded yet, and then displays the rest of the content over time. You could use the async pipe for this.

I would plan to load something small immediately, and, once that’s done, to load everything else.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:52pm UTC](https://forum.ionicframework.com/t/ionic-storage-as-promise-with-providing-initial-data/119655/7 "2021-07-19T21:52:59Z")

</div>

Closed on July 19, 2021, 9:52pm UTC


