title: How do I fix a race condition in a nested Promise in Node.js?
body: <p>Alright, so this question is an extension of <a href="https://stackoverflow.com/questions/44443563/nested-promise-is-not-propagating-error-to-parent-promise-in-node-js">this question</a> and related to <a href="https://stackoverflow.com/questions/46497131/how-do-i-skip-a-callback-when-running-map-in-javascript-node-js">this question</a>. My code calling the function format can be seen in the first question. This is the code in my transform:</p>
  
  <pre><code>format() {
      return new Promise((resolve, reject) =&gt; {
          return Database.getInstance().getModel('feature_default').findAll({})
          .then((databaseObjects) =&gt; {
              this.getJsonObject().features = {};
              for (let databaseObject of databaseObjects) {
                  console.log('ADD FEATURE DEFAULT TO ARRAY ' + databaseObject.id);
                  this.getJsonObject().features[databaseObject.id] = JSON.parse(databaseObject.default_json);
              }
              console.log('RESOLVE FEATURE DEFAULT');
              return resolve(this.getJsonObject());
          })
          .catch((e) =&gt; {
              return reject(e);
          });
      });
  }
  </code></pre>
  
  <p>I tried to remove the anti-pattern and I'm seeing the exact same behavior:</p>
  
  <pre><code>format() {
      return Database.getInstance().getModel('feature_default').findAll({})
          .then((databaseObjects) =&gt; {
              this.getJsonObject().features = {};
              for (let databaseObject of databaseObjects) {
                  console.log('ADD FEATURE DEFAULT TO ARRAY ' + databaseObject.id);
                  this.getJsonObject().features[databaseObject.id] = JSON.parse(databaseObject.default_json);
              }
              console.log('RESOLVE FEATURE DEFAULT');
              return this.getJsonObject();
          })
          .catch((e) =&gt; {
              throw e;
          });
  }
  </code></pre>
  
  <p>Here is the code calling format:</p>
  
  <pre><code>            var promises = databaseObjects.map(databaseObject =&gt; {
                  var jsonObject = {};
  
                  var transform = new Transform();
                  transform.setDatabaseObject(databaseObject);
                  transform.setJsonObject(jsonObject);
  
                  transform.baseFormat()
                  .then(() =&gt; transform.format())
                  .then(() =&gt; {
                      res.locals.retval.addData(jsonObject);
                      res.locals.retval.addErrors(transform.getErrors());
                      res.locals.retval.addWarnings(transform.getWarnings());
                  }).catch((e) =&gt; {
                      console.log('Caught error during format of existing object: ');
                      console.log(e);
                      this.error(e, res);
                      throw e;
                  });
              });
              Promise.all(promises)
              // ...
  </code></pre>
  
  <p>I've tried to do a <code>Promise.all</code> like in my second question, where each promise adds a feature to the array, but that did not fix this issue either.</p>
  
  <p>Using this call, when I first query the server, I get the correct result:</p>
  
  <pre><code>{
    "errors": [],
    "warnings": [],
    "data": [
      {
        "id": 1,
        "name": "All",
        "enabled": 1,
        "features": {
          ...
        }
      }
    ]
  }
  </code></pre>
  
  <p>However, on any subsequent calls I receive this:</p>
  
  <pre><code>{
    "errors": [],
    "warnings": [],
    "data": []
  }
  </code></pre>
  
  <p>I am not changing any parameters or the call at all. I believe I'm failing to lock a Promise but I don't see where as I'm returning everywhere I should.</p>