title: JS: Async/await doesn't seem to wait for result in a loop
body: <p>I've got an async map function. Within this I'm creating a set of documents, if they are not existing.
  The ID of the first created document should be set as <code>parent</code> for the next document.</p>
  
  <pre><code>await Promise.all(data.map(async (o, index) =&gt; {
    const key = o.name
    const value = o.value
    const query = { [key]: value }
    const parentID = refID
  
    console.log(parentID);
  
    if (parentID) query.parent = parentID
  
    // Check if document is existing
    let ref = await References.findOne(query)
    refID = ref ? ref._id : undefined
  
    // If not existing, create it
    if (!refID) {
      const refInserted = await References.insertOne(query)
      refID = refInserted ? refInserted.insertedId : undefined
    }
  
    console.log(refID)
  }))
  </code></pre>
  
  <p>So I would expect this log output with alternating refID and parentID:</p>
  
  <pre><code>undefined (parentID first run)
  yBZWYJcWBoSzx9qwy (refID first run)
  yBZWYJcWBoSzx9qwy (parentID second run)
  23poGAbg85LCqkcZc (refID second run)
  23poGAbg85LCqkcZc (parentID third run)
  yBJYzedxftLe2Xm4r (refID third run)
  yBJYzedxftLe2Xm4r (parentID fourth run)
  PpzjuZ4vfrktNH4ez (refID fourth run)
  </code></pre>
  
  <p>But I do get</p>
  
  <pre><code>undefined
  undefined
  undefined
  undefined
  undefined
  undefined
  yBZWYJcWBoSzx9qwy
  23poGAbg85LCqkcZc
  yBJYzedxftLe2Xm4r
  PpzjuZ4vfrktNH4ez
  </code></pre>
  
  <p>which shows me that it is not alternating the log output, but running in two blocks although I've set <code>await</code> for the <code>insertOne</code> (using mongo native driver). So obviously I'm misunderstanding something:</p>
  
  <p>What I think I'm doing is, that <code>map</code> is - in this case - the same like an forEach loop:
  After the first run the ID of the existing document or the ID of the new created document (if not exiting) is extracted.
  In the second run, this ID is set as parentID so for the second created document the parent key is set.</p>