title: Javascript : promise chain vs. async/await?
body: <p>I am learning about Javascript <code>Promise</code> and <code>async</code>/<code>await</code>. The sample code below asynchronously reads and parses a JSON file in node.js (<strong>my node.js version is v10.0.0</strong>).</p>
  
  <p>In the sample code, ChainReadJson function and AwaitReadJson function are doing the same thing, reading and parsing a JSON file. The difference is that ChainReadJson function uses a promise chain, while AwaitReadJson function uses async/await.</p>
  
  <pre class="lang-js prettyprint-override"><code>const FS = require("fs");
  
  function ReadFile(fileName) {
      return new Promise((Resolve, Reject) =&gt; {
          FS.readFile(fileName, 'utf8', (error, result) =&gt; {
              if (error)
                  Reject(error);
              else
                  Resolve(result);
          });
      });
  }
  
  // function using promise chain
  
  function ChainReadJson(fileName, CallBack) {
      ReadFile(fileName)
          .then(
              res =&gt; JSON.parse(res),
              err =&gt; {
                  Message(-1, err.message);
              }
          )
          .then(
              res =&gt; {
                  if (res !== undefined)
                      CallBack(fileName, res);
              },
              err =&gt; {
                  Message(-2, err.message);
              }
          );
  }
  
  // function using async/await
  
  async function AwaitReadJson(fileName, CallBack) {
      let res, json;
  
      try {
          res = await ReadFile(fileName);
      }
      catch (err) {
          Message(-1, err.message);
          return;
      }
      try {
          json = JSON.parse(res);
      }
      catch (err) {
          Message(-2, err.message);
          return;
      }
      CallBack(fileName, json);
  }
  
  ChainReadJson('test.json', PrintJSON);
  AwaitReadJson('test.json', PrintJSON);
  
  // common functions
  
  function PrintJSON(fileName, json) {
      console.log(`JSON[${fileName}]:`, json);
  }
  
  function Message(n, str) {
      console.log(`[${n}]`, str);
  }
  </code></pre>
  
  <p>When writing the code for ChainReadJson function using promise chain, I had difficulties controlling execution results and errors. However, when writing the code for AwaitReadJson function using async/await, those difficulties are mostly disappeared.</p>
  
  <p>Do I correctly understand the benefits of async/await? What are the disadvantages of async/await compared to promise chain?</p>
  
  <p>(The sample code is a modified version of <a href="https://stackoverflow.com/a/50210073/7339376">the code in this answer</a>. The original code uses promise chain only, and is written to know exactly where in the chain the error occurred and what is the error)</p>