title: Promise.all always returns an empty array
body: <p>I'm trying to solve this promise puzzle and I've had 2 questions:</p>
  
  <p>A) I'm wondering why it's returning empty array.  What am i doing wrong?</p>
  
  <p>B) How can I implement async reduce?</p>
  
  <p>B) How can I make it return a Async Array instead of empty array?</p>
  
  <p><strong>Note:</strong></p>
  
  <blockquote>
    <p>Please make use of <code>.get</code> method to iterate through the elements and
    the return value must be a asyncArray (not a regular array)</p>
  </blockquote>
  
  <p>Code looks like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  /**
       * Async array.
       */
      function AsyncArray(arr) {
        this._arr = arr;
        this.length = arr.length;
      }
      
      /**
       * Asynchronously get the array item of the
       * given index.
       * @param {number} index - array index of the desired item
       * @param {function} callback - called with the array item
       */
      AsyncArray.prototype.get = function get(index, callback) {
        setTimeout(callback, 0, this._arr[index]);
      };
      
      
      /**
       * Async version of Array.prototype.map.
       * @param {AsyncArray} arr
       * @param {function} fn - (item: any) =&gt; any
       * @returns {Promise&lt;AsyncArray&gt;}
       */
      function asyncMap(arr, fn) {
        
        let counter = 0; // counter
        const res = []; /// array of promises.
        const len = arr.length;
        
        // Get the length.
        return new Promise((resolve, reject) =&gt; { // Pending.
          
          while(true) {
            if(counter===len) {
              console.log("before break", res);
              break;
            }
            
            arr.get(counter, item =&gt; {
              res[counter] = function() {
                return new Promise((resolve, reject) =&gt; {
                  return resolve(fn(item));
                });
              }();
              
              console.log('r',res);
            });
            counter += 1;
          }
        
      
          Promise.all(res).then((r1, rej) =&gt; {
            console.log("hello world", r1);
            return resolve(res); 
          });
        });
      }
      
      /**
       * Async version of Array.prototype.reduce.
       * @param {AsyncArray} arr
       * @param {function} fn - (val: any, item: any) =&gt; any
       * @returns {Promise&lt;any&gt;}
       */
      function asyncReduce(arr, fn, initVal) {}
      
      
      const arr = new AsyncArray([1, 2, 3]);
      
      // arr.get(1, item =&gt; console.log(item)); // Existing
      
      // Expected result: [2, 4, 6];
      asyncMap(arr, x =&gt; x * 2).then(arr_ =&gt; console.log('asyncMap:', arr_));
      
      // Expected result: 106
      // asyncReduce(arr, (v, x) =&gt; v + x, 100).then(val =&gt; console.log('asyncReduce:', val));
      </code></pre>
  </div>
  </div>
  </p>