# Ionic 3 method not pushing to array

**URL:** <https://forum.ionicframework.com/t/ionic-3-method-not-pushing-to-array/86863>\
**Category:** ionic-v3\
**Created:** [April 18, 2017, 1:35am UTC](https://forum.ionicframework.com/t/ionic-3-method-not-pushing-to-array/86863 "2017-04-18T01:35:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![csuarezgfx](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/csuarezgfx/32/106250_2.png) [@csuarezgfx](https://forum.ionicframework.com/u/csuarezgfx)\
**Post date:** [April 18, 2017, 1:35am UTC](https://forum.ionicframework.com/t/ionic-3-method-not-pushing-to-array/86863/1 "2017-04-18T01:35:58Z")

</div>

Hi, I’m trying to push data into an array and for some reason it produces a void. This is the method:

```
getAllFavorites() {
  let items = [];

  console.log('Getting all favs...')
  this.storage.ready().then(() => {
     this.storage.forEach( (v, k) => {
        items.push(JSON.parse(v));
        console.log(JSON.parse(v))
     });
  });

 return items.length ? items : null;

```

}

and then on:  
onViewDidEnter() {  
this.favorites = this.userSettings.getAllFavorites();  
**console.log("Favs: ", this.favorites);**  
}

**During the forEach callback, It prints out fine but the push method does nothing. Calling the getAllFavorites returns null or a void/undefined if I return the items array directly. What am I doing wrong?**

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/f/d/fd1813d0a3ad7dc7b14dfb27e2844d4e1463b061.png)

---

<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:** [April 18, 2017, 1:54am UTC](https://forum.ionicframework.com/t/ionic-3-method-not-pushing-to-array/86863/2 "2017-04-18T01:54:23Z")

</div>

You’re checking the value of this.favorites before the Promise completes.

---

<div class="post-metadata">

**Author:** ![csuarezgfx](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/csuarezgfx/32/106250_2.png) [@csuarezgfx](https://forum.ionicframework.com/u/csuarezgfx)\
**Post date:** [April 18, 2017, 1:58am UTC](https://forum.ionicframework.com/t/ionic-3-method-not-pushing-to-array/86863/3 "2017-04-18T01:58:16Z")

</div>

I don’t think that is it. I just printed the length of the items array after the forEach method and it returned: 0

```
this.storage.ready().then(() => {
     this.storage.forEach( (v, k) => {
        items.push(JSON.parse(v));
        console.log(JSON.parse(v))
     });
     console.log("length: ",items.length);
});
```

---

<div class="post-metadata">

**Author:** ![SigmundFroyd](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sigmundfroyd/32/111604_2.png) [@SigmundFroyd](https://forum.ionicframework.com/u/SigmundFroyd)\
**Post date:** [April 18, 2017, 2:00am UTC](https://forum.ionicframework.com/t/ionic-3-method-not-pushing-to-array/86863/4 "2017-04-18T02:00:08Z")

</div>

Alrighty, so. [Promises.](http://learnangular2.com/es6/promises)

Promises are asynchronous. Which in short means it finishes at some point. It may finish now. It may finish 5 years from now. We’re not sure though.

In your code you have:

```auto
this.storage.ready().then(() => {
//..my code here
});

```

This says, hey, storage. Whenever you’re ready, lemme know so that I can execute `//..my code here`.

This is great! However, that means it probably hasn’t executed when you get to the line

```auto
return items.length ? items : null;

```

Why? Because it’s a promise. It’ll get back to you, it promises!, but not yet.

So! How do we fix this?

You’ll have to change `getAllFavorites` to something like this:

```auto
getAllFavorites() {
  console.log('Getting all favs...')
  return this.storage.ready().then(() => {
     let items = [];

     this.storage.forEach( (v, k) => {
        items.push(JSON.parse(v));
        console.log(JSON.parse(v))
     });

     return items.length ? items : null;
  });
}

```

Then, you’ll have to change `ionViewDidEnter` to:

```auto
ionViewDidEnter() {
    this.userSettings.getAllFavorites(faves => {
        this.favorites = faves;
        console.log("Favs: ", this.favorites);
    });
}

```

---

<div class="post-metadata">

**Author:** ![csuarezgfx](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/csuarezgfx/32/106250_2.png) [@csuarezgfx](https://forum.ionicframework.com/u/csuarezgfx)\
**Post date:** [April 18, 2017, 2:02am UTC](https://forum.ionicframework.com/t/ionic-3-method-not-pushing-to-array/86863/5 "2017-04-18T02:02:30Z")

</div>

Ahhhh, ok. It is the async timing thingy. I come from the c++ world and this async programming still throws me off. Thank you. That makes complete sense.
