Javascript Promise().then to prevent re-calling the function before the first call be executed
<p>In my node.js app, reading data from MSSQL using tedious, I'm calling the below every 1 second:</p>

<ol>
<li>Fetch the data from the server (<code>fetchStock</code> function) and save it in temporary array </li>
<li>Send the data saved in the temporary array to the client using the Server-Sent Events (SSE) API.</li>
</ol>

<p>It looks the 1 second is not enough to recall the <code>fetchStock</code> function before the previous call is completely executed, so I get execution errors from time to time.
I increased it to 5 seconds, but still get the same issue every once in a while.</p>

<p>How can I use <code>Promise().then</code> to be sure the <code>fetchStock</code>  function is not re-called before the previouse call be completely executed?</p>

<pre><code>var Request = require('tedious').Request;
var Connection = require('tedious').Connection;
var config = {
     userName: 'sa',
     password: 'pswd',
    server: 'xx.xxx.xx.xxx',
    options: {
        database: 'DB',
             rowCollectionOnRequestCompletion: 'true',
             rowCollectionOnDone: 'true'
     },
 };
var sql = new Connection(config);

var addElem = (obj, elem)=&gt; [].push.call(obj, elem);
var result = {}, tmpCol = {}, tmpRow = {};
module.exports = {

  displayStock: function (es) {
      var dloop = setInterval(function() {
      if(result.error !== null)
            if (es) es.send(JSON.stringify(result), {event: 'rmSoH', id: (new Date()).toLocaleTimeString()});
      if(result.error === null)
            if (es) es.send('connection is closed');
      }, 1000);
  },

  fetchStock: function () {
            request = new Request("SELECT ItemCode, WhsCode, OnHand FROM OITW where OnHand &gt; 0 and (WhsCode ='RM' or WhsCode ='FG');", function(err, rowCount, rows) {
            if (err) {
            result = {'error': err};
            console.log((new Date()).toLocaleTimeString()+' err : '+err);
             }
             if(rows)
             rows.forEach(function(row){
                 row.forEach(function(column){
                     var colName = column.metadata.colName;
                     var value = column.value;
                     addElem(tmpCol, {colName: value})
                 });
             addElem(tmpRow,{'item': tmpCol[0].colName, 'Whs': tmpCol[1].colName, 'Qty': tmpCol[2].colName});
             tmpCol = {};
           });
           result = tmpRow;
           tmpRow={}
       });
    sql.execSql(request);
  }
}
</code></pre>