title: bcrypt hash all password in array
body: <p>I have an array of objects with &quot;users&quot; that each have a username, name and password property. I want to hash each user's password, create a User object with same properties but omit the password for a hash, and then return it using map() before saving and then finish it off with Promise.all()</p>
  <pre class="lang-js prettyprint-override"><code>const initialUsersArray = [
    {
      username: 'user1',
      name: 'User McUser',
      password: 'abcd'
    },
    {
      username: 'user2',
      name: 'User Usersson',
      password: '1234'
    },
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>const SALT_WORK_FACTOR = 10;
  
  const hashedUsers= initialUsersArray.map(async (user) =&gt; {
      const hashedPassword = await bcrypt.hash(user.password, SALT_WORK_FACTOR);
      return new User({
        username: user.username,
        name: user.name,
        hashedPassword 
      });
    });
    const promiseArray = hashedUsers.map(user =&gt; user.save());
    await Promise.all(promiseArray);
  </code></pre>
  <p>The issue is that it doesn't wait for the promises to resolve before trying to save() each user.
  If I console.log hashedUsers I get an array of <code>Promise { &lt;pending&gt; },</code></p>
  <p>I'm missing something on how Promises works inside map(),
  above works just fine if I just use it for a single user, like this</p>
  <pre class="lang-js prettyprint-override"><code>const hashedPassword = await bcrypt.hash(password, SALT_WORK_FACTOR);
  const user = new User({
    username: username,
    name: name,
    hashedPassword,
  });
  await user.save()
  </code></pre>