title: Returning a Promise in Node.js
body: <p>I can't wrap my head around this.</p>
  
  <p>Take the following three functions where <code>a</code> calls <code>b</code>, and <code>b</code> calls <code>c</code>:</p>
  
  <pre><code>const c = () =&gt; {
      return new Promise( (resolve, reject) =&gt; {
          setTimeout(() =&gt; {
              resolve(new Date());
          }, "1750");
      });
  };
  
  const b = async () =&gt; {
      const result = await c();
      console.log("b(): %s", result);
      return result;
  };
  
  const a = async () =&gt; {
      const result = await b();
      console.log("a(): %s",result);
      return result;
  };
  
  console.log("Starting...");
  const final_result = a();
  console.log("Final Result: %s", final_result);
  console.log("Ending...");
  </code></pre>
  
  <p>I expected <code>b()</code> to wait-for/get the result of the promise returned by <code>c()</code>, and pass the value to <code>a()</code>. However, it looks like the promise gets passed all the way up the call stack.</p>
  
  <p><a href="https://i.stack.imgur.com/bmvNl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bmvNl.png" alt="enter image description here"></a></p>
  
  <p>To get the behavior I want, I have to handle the promise in each function:</p>
  
  <pre><code>const c = () =&gt; {
      return new Promise( (resolve, reject) =&gt; {
          setTimeout(() =&gt; {
              resolve(new Date());
          }, "1750");
      });
  };
  
  const b = async () =&gt; {
      const result = await c();
      console.log("b(): %s", result);
      return result;
  };
  
  const a = async () =&gt; {
      const result = await b();
      console.log("a(): %s",result);
      return result;
  };
  (async () =&gt; {
      console.log("Starting...");
      const final_result = await a();
      console.log("Final Result: %s", final_result);
      console.log("Ending...");
  })()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/SoGZx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SoGZx.png" alt="enter image description here"></a></p>
  
  <p>Why can't I just get the result of the promise in one function and return that? And, if I can do it, how?</p>