Promise.all is returning an array of undefined and resolves before being done

Promise.all accepts an Array of Promise objects.
You’re getting an Array of undefined because you’re not returning any Promise in your map callback:

function addText(queries) {
  return Promise.all(queries.map(function(query) {
    // Add `return` here or the `map` returns an Array of `undefined`.
    return models.queries
      .findById(query.queryId, {
        raw: true,
        attributes: [ "query" ]
      })
      .then(function(queryFetched) {
        query.text = queryFetched.query;
        console.log(query);
        
        return Promise.resolve(query);
      }, function(error) {
        return Promise.reject(error);
      });
  }));
};

A primitive value such as undefined will resolve immediately in Promise.all, therefore it resolves before any of the Promises in your callback resolve.

Leave a Comment