title: How can I get a function to execute every time it receives output from another function? (NodeJS)
body: <p>I have some pseudo code below:</p>
  
  <pre><code>function generateNumber() {
    const n = Math.floor(Math.random() * 15);
    console.log('Original Number')
    console.log(n)
    return {
      n
    };
  }
  
  function loop() {
    const rand = Math.round(Math.random() * (4000 - 600)) + 600;
    setTimeout(() =&gt; {
      generateNumber();
      add_1();
      loop();
  
    }, rand);
  }
  
  function add_1() {
    const x = generateNumber() + 1;
    console.log('New Number')
    console.log(x)
  }
  
  loop()
  </code></pre>
  
  <p>What I want is for <code>add_1()</code> to execute every time <code>generateNumber()</code> outputs a number. So for now, <code>generateNumber()</code> will output a value and it immediately goes to <code>add_1()</code>, where a value of 1 is added to the incoming number, which is then sent back out. The <code>loop()</code> function is there just to get <code>generateNumber()</code> to continuously output values at different times. But in reality it could be that I want a sensor to provide data, and every time it sends a value, I want the data to go into a function (i.e. <code>add_1()</code>).</p>
  
  <p><strong>Is my only option to have <code>generateNumber()</code> and <code>add_1()</code> in a function together? (Which is the case in the pseudo code above)</strong> </p>
  
  <p>I want it so that <code>add_1()</code> executes every time it gets input. <strong>Preferably without needing <code>add_1()</code> to be in a function, or loop, with <code>generateNumber()</code>.</strong> </p>
  
  <p>My guess is <code>add_1()</code> has to be an async function and has to wait for some sort of promise from <code>generateNumber()</code>.</p>
  
  <p>You can see in the code, that there is nothing to "trigger" <code>add_1()</code> to execute, so I believe my question is how to create this "trigger" based on output from <code>generateNumber()</code>.</p>