1

I initialized an array and got the data through a function call that returns a promise. In the then I populated the array and printing the array works fine. Outside of the promise I try to access the array and it's back to being empty. How can I keep the changes made in the then function?

var arr : IHash = {};
this.getTags(parameters)
    .then((data) => {
         for (var t in data.Tags) {
             arr[t] = data.Tags[t];
             this.$log.debug(arr["firstindex"]); //outputs data
         }
         }, (err) => {
             this.$log.debug(err);
         });

        this.$log.debug(arr["firstindex"]); //prints nothing
mysticalstick
  • 2,479
  • 5
  • 16
  • 21
  • Your first log statement prints after your second log statement. That should tell you what's going on, this is asynchronous code. – Ruan Mendes Feb 15 '17 at 17:10

1 Answers1

1

The Promise is asynchronous, so this.$log.debug(arr["firstindex"]); is getting called before the array is populated.

hobberwickey
  • 6,118
  • 4
  • 28
  • 29