title: Async fetch in loop
body: <p>I have an asynchronous operation, fetching an array of items:</p>
  
  <pre><code>Base.find({ creatorId: req.params.id })
  </code></pre>
  
  <p>Each item in that array has an ID, with which I need to perform a fetch of messages  and users for that item. For that I need a loop:</p>
  
  <pre><code>Base.find({ creatorId: req.params.id })
          .then(bases =&gt; {
              for (let base of bases) {
              }
          })
  </code></pre>
  
  <p>Bsae.find() will return multiple items, hence the loop. The end of the GET method in node will have to return an array that looks something like:</p>
  
  <pre><code>bases: [
    {
     base: {info about base},
     users: 12,
     messages: 51
     },
     ...another base...,
     ...another base...
  ]
  </code></pre>
  
  <p>So far I have been unable to work multiple Promises (.find methods) into the loop, and have been unable to <code>serialize()</code> the information in a follow-up <code>.then()</code>. I have read other posts on stack overflow in which they suggested to use <code>async/await</code> functions, but this only let me perform one of the two methods (<code>baseUser.find()</code>). I don't know how perform these operations for each of the bases which are initially returned by <code>Base.find()</code></p>
  
  <p><strong>EDIT</strong>:
  For clarification, I need to call the following functions:
  <code>Base.find</code> => returns an array of bases.
  <code>BaseUser.find()</code> => which needs to be in a loop, because i need to <code>find</code> each item with an Id from the first <code>find</code>.
  <code>Message.find()</code> => same as baseuser, messages get fetched with an ID from <code>Base.find()</code>.</p>
  
  <p><strong>EDIT2</strong>:
  I now receive some data, but mostly useless for me to work with since I don't know of any way to data from the following:</p>
  
  <pre><code>async function fetcher() {
      try {
          const bases = await Base.find({ creatorId: req.params.id });
          const baseUserPromises = bases.map(base =&gt;
              BaseUser.find({ baseId: base._id })
          );
          return await Promise.all(baseUserPromises);
      } catch (err) {
          console.error(err);
          res.status(500).json({ message: "Internal server error" });
      }
  }
  fetcher().then(data =&gt; console.log(data))
  </code></pre>
  
  <p>result: </p>
  
  <pre><code>[ [ { created: 2018-09-03T14:05:39.968Z,
        acceptedMembership: true,
        isCreator: true,
        _id: 5b8d3fb3ccdfcc2e20540604,
        userId: 'tester1',
        baseId: 5b8d3fb3ccdfcc2e20540603},
      { created: 2018-09-03T14:05:43.357Z,
        acceptedMembership: false,
        isCreator: false,
        _id: 5b8d3fb7ccdfcc2e20540605,
        userId: 'tester2',
        baseId: 5b8d3fb3ccdfcc2e20540603},
      { created: 2018-09-03T14:05:44.797Z,
        acceptedMembership: false,
        isCreator: false,
        _id: 5b8d3fb8ccdfcc2e20540606,
        userId: 'tester3',
        baseId: 5b8d3fb3ccdfcc2e20540603 } ],
    [ { created: 2018-09-03T14:11:48.802Z,
        acceptedMembership: true,
        isCreator: true,
        _id: 5b8d4124e96ca00eb42df8c9,
        userId: 'tester1',
        baseId: 5b8d4124e96ca00eb42df8c8 } ] ]
  </code></pre>
  
  <p>Double arrays since <code>Promise.all()</code> returns the first array with the resolved Promises, and then Mongo's <code>collection.find()</code> returns an array as well. So it's a Promise array, filled with two <code>find</code> arrays, filled with the results from <code>find</code></p>