title: Forcing Promise resolve order
body: <p>I have a parser function <code>macroBuilder()</code> going through text and parsing macros.
  Some macros resolve to text, and there are also nested macros that require another pass. The function returns a string.</p>
  
  <p>I deal with nesting as:</p>
  
  <pre><code>let fragments = [];
  fragments.promises = [];
  fragments.push = function() {
    let fragments = this;
    for (let i = 0; i &lt; arguments.length; i++) {
      if (typeof arguments[i].then === 'function') {
        let id =  Date.now() + i + this.length;
        let promise = arguments[i]
          .then( function(id, val) {
            fragments[fragments.indexOf(id)] = val;
          }
          .bind(undefined, id))
          .catch( function(id, err) {
            fragments[fragments.indexOf(id)] = '';
          }
          .bind(undefined, id) );
        this.promises.push(promise);
        arguments[i] = id;
      }
    }
    return Array.prototype.push.apply(this, arguments);
  }
  </code></pre>
  
  <p>At the end, I do:</p>
  
  <pre><code>await Promise.all(fragments.promises).then(pieces =&gt; {
    console.log('Promise.all(fragments.promises) resolved');
  });
  </code></pre>
  
  <p>Then when I go through my tokenized text, in a loop, I do:</p>
  
  <pre><code>textMacro(obj) {
    fragments.push(obj['text'])
  },
  anotherMacro(obj) {
    ...
  },
  nestedMacro(obj) {
    fragments.push(
      resolveMacro(obj['name'])
        .then(res =&gt; {
            return macroBuilder(res);
        }
    );
  }
  </code></pre>
  
  <p>This works fine, expect that my macros that get decoded as text are pushed into the <code>fragments</code> array immediately, while nested macros are decoded on the next pass after all regular macros have been already resolved to text.</p>
  
  <p>I need macros to be processed in the order in which they appear, because processing some macros have side effects and those need to be accounted for, then the next in the order macro is being processed.</p>
  
  <p>Tried dealing with this by converting regular text results into promises so <code>textMacro()</code> function looks like the below:</p>
  
  <pre><code>textMacro(obj) {
    fragments.push(
      new Promise((resolve, reject) =&gt; {
        resolve(obj['text']);
      })
    );
  }
  </code></pre>
  
  <p>This way, I hoped everything gets resolved in the correct order. However, my <code>textMacro()</code> and other not-nested type macro resolvers have the Promise resolved right away.</p>
  
  <p>What am I doing wrong?</p>