title: How return a value from an asynchronous function with SQLite3 and node.js?
body: <p>I'm trying to make a small database using SQLite3 for my discord bot (on node.js), and I'd like to make a function that updates the database with provided values (this part works) or return a value of the database.</p>
  <p>My problème is here, the returned value is always undefined...</p>
  <p>I have understood that SQLite3 commands are asynchronous so all the functions become asynchronous, but how I can make sure that the returned value be no longer undefined?</p>
  <p>Here is my code :</p>
  <pre class="lang-js prettyprint-override"><code>const sqlite3 = require('sqlite3');
  const dbname = 'dbStocks';
  
  module.exports = function gestionBDD(update, nomArticle, quantitéArticle) {
   console.log('1 ' + quantitéArticle);
  
   let db = new sqlite3.Database(dbname, (err) =&gt; {
    if (err) throw err;
    console.log('Démarrage de ' + dbname);
   });
  
   db.serialize(() =&gt; {
    nomArticle = '&quot;' + nomArticle + '&quot;';
  
    if (update) {
     //Si update est true : maj des quantités via le tableau articles
  
     db.run(
      `UPDATE stocks
                  SET Quantité = (` +
       quantitéArticle +
       `)
                  WHERE Article = (` +
       nomArticle +
       `)`
     );
    } else if (!update) {
     db.each(
      `SELECT Quantité FROM stocks WHERE Article  = (` + nomArticle + `)`,
      (err, data) =&gt; {
       console.log('2 ' + data.Quantité);
       return data.Quantité;
      }
     );
    }
  
    console.log('4 ' + quantitéArticle);
    db.close((err) =&gt; {
     if (err) throw err;
     console.log('Arrêt de ' + dbname);
    });
   });
  };
  </code></pre>
  <p>And I get this result:</p>
  <pre class="lang-none prettyprint-override"><code>1 undefined
  4 undefined
  3 undefined
  Démarrage de dbStocks
  2 500
  Arrêt de dbStocks
  </code></pre>
  <p>(the <code>3 undefined</code> is a <code>console.log()</code> in my <code>index.js</code> where my function <code>gestionBDD</code> is call)</p>