title: return value from a function with asynchronous commands
body: <p>I'm writing a NodeJS v0.10 application with MariaSQL.</p>
  
  <p>i want to create a function that returns the id of a row, and if the row doesn't exist, to create it and then return the id.</p>
  
  <p>this is what I have so far:</p>
  
  <pre><code>TuxDb.prototype.createIfNEDrinkCompany = function(drinkCompany) {
   this.client.query("insert into drink_company(drink_company_name) values(:drink_company) on duplicate key update drink_company_id=drink_company_id",
      {'drink_company' : drinkCompany})
      .on('result',function(res) {
      res.on('end',function(info){
          if (info.insertId &gt; 0) {
                  return info.insertId;
          } else {
              this.client.query("select drink_company_id from drink_company where drink_company_name = :drink_company",{'drink_company' : drinkCompany})
                  .on('result',function(res){
                      res.on('row',function(row){
                          return row.drink_company_id;
                      });
                  });
          }
      });
      });
  
  }
  </code></pre>
  
  <p>now the problem is that since it's asynchronous, the function ends before the value is returned. </p>
  
  <p>how can I resolve this issue ?</p>