title: Why use process.nextTick to ensure correct execution of asynchronous tasks?
body: <p>I am following an example of a book (<a href="https://www.amazon.es/Node-js-Design-Patterns-production-grade-applications/dp/1839214112/ref=pd_lpo_14_t_0/259-1666535-2336308?_encoding=UTF8&amp;pd_rd_i=1839214112&amp;pd_rd_r=61eb9590-ca46-4930-9157-472611f38861&amp;pd_rd_w=MxE2O&amp;pd_rd_wg=0vRj4&amp;pf_rd_p=4221015a-01c7-4a3d-a84d-985d938e9995&amp;pf_rd_r=KS4D7AN6ATNDFGVMBSPM&amp;psc=1&amp;refRID=KS4D7AN6ATNDFGVMBSPM" rel="nofollow noreferrer">Node.js design patterns</a>) to implement a <strong>LIMITED PARALLEL EXECUTION</strong> algorithm in Node.js with Javascript.</p>
  <p>First, I write a TaskQueue class that handles all the limiting and execution of the tasks.</p>
  <pre><code>class TaskQueue {
      constructor(concurrency) {
          this.concurrency = concurrency;
          this.running = 0;
          this.queue = [];
      }
      runTask(task) {
          return new Promise((resolve, reject) =&gt; {
              this.queue.push(() =&gt; {
                  return task().then(resolve, reject);
              });
              process.nextTick(this.next.bind(this));
          });
      }
      next() {
          while (this.running &lt; this.concurrency &amp;&amp; this.queue.length) {
              const task = this.queue.shift();
              task().finally(() =&gt; {
                  this.running--;
                  this.next();
              });
              this.running++;
          }
      }
  }
  </code></pre>
  <p>Then I have written a function that returns a promise that resolves after a random number of miliseconds and keeps record of the promise with an assigned number.</p>
  <p>This function delegates the task to the queue, wrapping the promise in a function that will be executed on demand by the TaskQueue class.</p>
  <pre><code>function promiseResolveRandomValue(promiseNumber, queue) {
      return queue.runTask(() =&gt; {
          return new Promise(resolve =&gt; {
              const random = Math.round(Math.random()*1000);
              setTimeout(() =&gt; {
                  console.log('Resolved promise number: ', promiseNumber, ' with delay of: ', random);
                  resolve();
              }, random);
          });
      })
  }
  </code></pre>
  <p>Finally I have used a map to execute the function over an array of ten numbers that will help us keep track of each promise.</p>
  <pre><code>const queue = new TaskQueue(2);
  const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  const promises = array.map((number) =&gt; {
      return promiseResolveRandomValue(number, queue);
  });
  
  Promise.all(promises)
      .then(() =&gt; console.log('Finished'));
  </code></pre>
  <p>So far so good. The thing is that the author recommends this line of code to be executed with process.nextTick to avoid any Zalgo type of situation, but I don't see why, as this code executes exactly the same just invoking the next method of the class.</p>
  <pre><code>runTask(task) {
          return new Promise((resolve, reject) =&gt; {
              this.queue.push(() =&gt; {
                  return task().then(resolve, reject);
              });
              process.nextTick(this.next.bind(this)); // &lt;-------------------------------- WHY?
          });
      }
  </code></pre>
  <p>Could someone clarify why is it convenient to use the process.nextTick in some cases such as this one to avoid conflicts with synchronous and asynchronous code?</p>