title: NodeJS with puppeteer Error from Promise Async
body: <p>I'm developing community auto screen capture app with NodeJS. Actually, It works well with it's screencapture function When I wait for a while it seems got a bug and doesn't work anymore.</p>
  
  <p>I found the error message in console like this.</p>
  
  <pre><code>(node:23436) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'screenshot' of null
      at C:\Users\user\Downloads\Projects\Bander-Statistics\google.js:60:31
      at process._tickCallback (internal/process/next_tick.js:68:7)
  (node:23436) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:23436) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  same data
  </code></pre>
  
  <p>I think the problem are from async function. But I have to wait for a long time to see error message again and I don't know why is this working like this.</p>
  
  <p>This is my full code</p>
  
  <pre><code>const puppeteer = require('puppeteer');
  const cheerio = require('cheerio')
  const request = require('request');
  
  let postArray = [];
  let uniq = [];
  let initNumber;
  let prevNumber;
  
  function data() {
      (async () =&gt; {
          await request.get({
              url: 'https://www.clien.net/service/group/community'
          }, function (err, response, body) {
              if (err) {
                  throw err;
              }
              let $ = cheerio.load(body);
              initNumber = $('.list_item.symph_row').attr('data-board-sn');
          })
          if (initNumber === prevNumber) {
              console.log('same data'); // Same Data, Do nothing
              // prevNumber = initNumber; // Compare with prevNumber
          } else {
              postArray.push(initNumber); // Different Data put to Array
              console.log(postArray);
              prevNumber = initNumber; // Compare with prevNumber
          }
          uniq = postArray.reduce(function (a, b) { // Sometimes in postArray have duplicate value so remove it.
              if (a.indexOf(b) &lt; 0) a.push(b);
              return a;
          }, []);
          console.log(uniq);
      })()
  }
  
  setInterval(data, 1000); // Interval 1 second because new post's are really uploaded fast
  
  // screenshot function
  function screenshot() {
      (async () =&gt; {
          const browser = await puppeteer.launch();
          const page = await browser.newPage();
  
          // Adjustments particular to this page to ensure we hit desktop breakpoint.
          await page.setViewport({
              width: 1920,
              height: 100000,
              deviceScaleFactor: 1
          });
          if (uniq.length === 0) { // If postArray are 0, Do nothing
              console.log(`This time Array ${uniq.length}`)
              console.log('No Processing this time');
          } else {
  
              for (let i = 0; i &lt; uniq.length; i++) { //If postArray have value iterate all value to screenshot
                  await page.goto(`https://www.clien.net/service/board/park/${uniq[i]}`);
                  const overlay = await page.$('.post_view');
                  await overlay.screenshot({
                      path: `./image/${uniq[i]}.png`, // save file name with postarray value
                  }).catch((err) =&gt; console.log(`Processing Image Clear Array INIT again ${err}`)) // Browser Close)   
              }
              postArray = []; // Reset Array
              await browser.close();
          }
          postArray = []; // Reset Array
          await browser.close(); // Browser Close
      })();
  }
  
  setInterval(screenshot, 30000); // Interval 10 seconds
  
  </code></pre>