title: Recursive async delayed promise overlapping
body: <p>The use case is the following: I want to query an URL every X milliseconds until I get a positive reply.</p>
  
  <p>I'm trying to achieve this in JS (node) and have an issue with multiple such requests.
  If I start only one of those "chain calls" everything works fine.</p>
  
  <p>But if I try to run  a number of those in parallel the "last one wins" (and the first one is never resolved). I seem to be using the reference to the same object or something, but I can't figure it out.</p>
  
  <p>Note how the "masterCounter" and "id" variables get overwritten although they are passed as arguments by value.</p>
  
  <p>Here is the working code(where "SECOND" wins over "FIRST"):</p>
  
  <p>(you can run with node if you install request package <a href="https://www.npmjs.com/package/request" rel="nofollow noreferrer">https://www.npmjs.com/package/request</a>)</p>
  
  <pre><code>var req = require('request');
  
  const ASK_EVERY_MS = 500;
  const AT_MOST_MS = 5000;
  
  var childRecursive = (t, resolve, reject, t0, id, masterCounter) =&gt; {    
      var duration = process.hrtime(t0);    
      duration = (duration[0] * Math.pow(10, 9) + duration[1]) / Math.pow(10, 6)
      if (duration + t &gt; AT_MOST_MS) {
          reject('timeout');
      } else {
          var options = {
              url: 'http://www.google.com',
              json: false,
              headers: {
                  timeout: 1000
              }
          };
          callback = function (error, response, body) {
              if (!error) {
                  console.log('body', body.substring(0, 100));
                  if (masterCounter % 5 == 0) {
                      return resolve("success!!!");
                  } else {
                      return childRecursive(ASK_EVERY_MS, resolve, reject, t0, id, masterCounter + 1, req);
                  }
              } else {
                  console.log('problem with submission GET request: ' + error);
                  return reject(error);
              }
          }
          console.log(id + ' masterCounter is ' + masterCounter);
          setTimeout(() =&gt; {
              console.log(id + ' requesting...');
              req.get(options, callback);
          }, t);
      }
  };
  
  var t0 = process.hrtime();
  var first = new Promise((resolve, reject) =&gt; childRecursive(0, resolve, reject, t0, 'FIRST', 101));
  first.then(() =&gt; {
      console.log('FIRST done');
  });
  var scnd = new Promise((resolve, reject) =&gt; childRecursive(0, resolve, reject, t0, 'SECOND', 201));
  scnd.then(() =&gt; {
     console.log('SECOND done');
  });
  </code></pre>
  
  <p>and the output:</p>
  
  <pre><code>FIRST masterCounter is 101
  SECOND masterCounter is 201
  FIRST requesting...
  SECOND requesting...
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 202
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 202
  SECOND requesting...
  SECOND requesting...
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 203
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 203
  SECOND requesting...
  SECOND requesting...
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 204
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 204
  SECOND requesting...
  SECOND requesting...
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 205
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND masterCounter is 205
  SECOND requesting...
  SECOND requesting...
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  SECOND done
  body &lt;!doctype html&gt;&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="hr"&gt;&lt;head&gt;&lt;meta content
  </code></pre>