0

I'm fetching data with promise. I'm using this callback function to loop through the value.

selectedAppIdCalculator() {
this.fetchData2().then( value => {

  console.log("value: ", value);


  for (let app of value) {
    console.log("in for loop");
  }
});
}

The response value is not null, because it logs first the value. The value is a array with json objects.

But the for loop is never executed. Why?

In the debugging mode I noticed something weird.

enter image description here

The value is empty. Also the value in the log console is empty: enter image description here

But when running normally it logs normally the response value. I'm really confused.

6
  • 1
    Could you post a sample of the content of value ? Commented Jun 6, 2018 at 10:48
  • don't use const type inside the for loop Commented Jun 6, 2018 at 10:49
  • 2
    @SachilaRanawaka Please Note: you definitely can use const in a for-of loop: const value = ['a', 'b', 'c']; for (const app of value) { console.log(app); } Commented Jun 6, 2018 at 10:54
  • @jozofe - the most likely issue is that value isn't an array... a common case would be where the array is deeper, i.e. value.someCollection. As Titian said, you need to share what is in value so we can answer the question. Commented Jun 6, 2018 at 10:57
  • @Fenton I edited my question Commented Jun 6, 2018 at 10:59

2 Answers 2

1

Ok all your problem is that you dont wait the fetchAppData response before resolving.

You resolve your promise after 100ms with this.reduceAppList as an empty Array. Later when fetchAppData response is received you populate this array. Thats why the data are displayed in the console ( since the console use Object reference to display data ) and not in debug mode (since the breakpoint is set before populating the Array.

You should change the fetchData2 function to this :

fetchData2() {
   this.reducedAppList = [];
   this.httpService.fetchAppData().then(response => {
       this.appList = this.httpService.statistics;
       for (const app of this.appList) {
           if (app.appMailings !== null) {
               this.reducedAppList.push(app);
           }
       }
       return this.reducedAppList;
   });
}
Sign up to request clarification or add additional context in comments.

Comments

0

It looks as though your resolve from fetchData2 is outside of the promise. If so, resolve is called before any data is available and needs to be just outside the scope of the for.

This is because of the async nature, and your resolve completes before the request is complete and returns instead the initialised value of reducedAppList

Should look something like this:

for (const app of this.appList) {
   // logic ommitted
}
resolve(this.reducedAppList); // move inside the `.then(response => ....`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.