title: Javascript, how to await multiple promises
body: <p>What I want to accomplish:</p>
  
  <ul>
  <li>gather artist id`s
  
  <ul>
  <li>either finding them in the db</li>
  <li>or creating them</li>
  </ul></li>
  <li>creating an event in the db, getting the event_id</li>
  <li>waiting till both are done, artists and event id`s gathered</li>
  <li>now looping over the artist, event combinations</li>
  </ul>
  
  <p>What I got:</p>
  
  <p>I`m working with Node and mysql. To insert the relations I have to wait for the artists to insert or create. I try to accomplish with the following code:</p>
  
  <pre><code>let promises = [];
  
  if (artists.length != 0) {
      for (key in artists) {
          promises.push( find_artist_id_or_create_new_artist(artists[key]) )
      }
  }
  
  await Promise.all(promises);
  </code></pre>
  
  <p>Returning an id:</p>
  
  <pre><code>async function find_artist_id_or_create_new_artist(artist_name) {
      return await find_artist_return_id(artist_name, create_artist_return_id)
  } 
  </code></pre>
  
  <p>Finding an artist:</p>
  
  <pre><code>async function find_artist_return_id(artist_name, callback) {
      var sql = "SELECT * FROM `artists` WHERE `name` LIKE "+con.escape(artist_name)+" LIMIT 1;"
  
      con.query(sql, (err,row) =&gt; {
        if(err) throw err;
  
        if (row.length == 0) {
          return callback(artist_name)
        } else {
          return row[0].id
        }
      });
  }
  </code></pre>
  
  <p>Creating an artist</p>
  
  <pre><code>async function create_artist_return_id(artist_name) {
      var sql = "INSERT INTO `artists` (`id`, `name`, `meta_1`, `meta_2`) VALUES (NULL, "+con.escape(artist_name)+", NULL, NULL)";
  
      con.query(sql, (err, result) =&gt; {
        if(err) throw err;
  
        return result.insertId
      });
  }
  </code></pre>
  
  <p>I understand that I cant return in a con.query function, but I dont how to properly setup the code to get this done. A link to, or help how to search for an answer is appreciated.</p>