title: NodeJs: Run Mysql queries synchronously
body: <p>I'm using NodeJS and mysql2 to store data in my database. but their are times when I need to perform database saves synchronously, like this example:</p>
  
  <pre><code>if(rent.client.id === 0){
      //Save client
      connection.query('INSERT INTO clients (name, identity, licence, birthDate, address, phoneNumber, email) VALUES (?,?,?,?,?,?,?)', 
                       [/*Array of values*/],
                       function (err, results) {
          if (err) throw err;
  
          //Retrieve client id to use it in the next database save
          rent.client.id = results.insertId;
      })
  }
  
  //Save rent
  connection.query('INSERT INTO rents (deliveryDate, returnDate, gasLevel, deliveryPoint, deliveryPrice, unitPrice, state, clientID, carID) VALUES (?,?,?,?,?,?,?,?,?)', 
                   [/*Array of values that contain the last inserted id clients*/],
                   function (err, results) {
      if (err) throw err;
      console.log('rent saved', results);
  })
  </code></pre>
  
  <p>So how can I perform these two database saves synchronously. I don't think that doing it in the following manner is good code:</p>
  
  <pre><code>connection.query(queryString, 
                   [queryValues],
                   function (err, results) {
      if (err) throw err;
      connection.query(queryString, 
                       [queryValues],
                       function (err, results) {
          if (err) throw err;
          console.log('rent saved', results);
      })
  
  })
  </code></pre>
  
  <p>So what kind of solutions do you propose?</p>