title: JavaScript array.push() inside promise .then()
body: <p>Currently, I struggle with some weird js array behavior, which is new to me. Would be great if someone can help me out with that.
  So this is my code:</p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>getData(strings) {
      let unsortedKeys = ['key1', 'key2'];
  
      strings.forEach(data =&gt; {
        ref.once("value").then(data =&gt; {
  
          // doesn't matter what will be pushed to the array
          unsortedKeys[2] = 'key3';
  
        });
      });
  
      return unsortedKeys;
  }
  </code></pre>
  
  <p>I know this code will not work. I've simplified the Firebase promise for this example. By the way, I'm using Angular with Typescript.</p>
  
  <p>The result:</p>
  
  <pre><code>console.log(getData());
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tHkkg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tHkkg.png" alt="enter image description here"></a></p>
  
  <p>If i know iterate over the array just the values of <code>[0]</code> and <code>[1]</code> will be shown. Also accessing the field via index <code>getData()[2]</code> is not possible. I'm not sure if this happens cause the async behavior of promises, but it seems really strange to me, since the value and the index are there.</p>
  
  <p>Does anyone have an idea why this happens? </p>