title: unhandled catch error in promise that has catch - Nodejs
body: <p>I have a function that GETs data from either mongodb or azure redis cache. I'm trying to set it up where it checks the cache first and then hits mongodb if cache is empty, but for some reason it's executing the mongodb code first. I created a promise to handle the order, but it's giving me an unhandled promise rejection error. I have a catch so I don't understand what the issue is. I appreciate any help! </p>
  
  <p>console output</p>
  
  <pre><code>
  SETTING TRUE...
  (node:27068) UnhandledPromiseRejectionWarning: Cache found
  (node:27068) 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:27068) [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.
  
  </code></pre>
  
  <p>GET request</p>
  
  <pre><code>
  
    const promise = new Promise((resolve, reject) =&gt; {
        client.get("directory", (err, reply) =&gt; {
          if (reply !== null) {
            console.log("SETTING TRUE...");
            redisReply = true;
  
            replyConverted = JSON.parse(reply);
  
            res.status(200).json({
              message: "Directory retrieved successfully!",
              posts: replyConverted,
              maxPosts: replyConverted
            });
            reject("Cache found);
          } else {
            resolve();
          }
        });
      }).then(res =&gt; {
        const postPerPage = 20;
        console.log("directory ID SECTION");
  
        var searchKey = new RegExp(req.query.keyword, "i");
  
        console.log("req.query.currentPage");
        console.log(req.query.currentPage);
        console.log(req.query.keyword);
        let currentPage = req.query.currentPage;
  
        console.log("REDIS RELY IS " + redisReply);
        console.log("GRABBING FROM COSMOSDB");
        User.countDocuments({
          $and: [{ username: searchKey }, { role: "Seller" }]
        })
          .then(docs =&gt; {
            console.log("COUNT");
            console.log(docs);
            let totalPosts = docs;
            User.find({
              $and: [{ username: searchKey }, { role: "Seller" }]
            })
              .select("_id username")
              .skip(postPerPage * (currentPage - 1))
              .limit(postPerPage)
              .then(documents =&gt; {
                res.status(200).json({
                  message: "Directory retrieved successfully!",
                  posts: documents,
                  maxPosts: totalPosts
                });
  
                let docsString = JSON.stringify(docs);
                client.set("directory", docsString, (err, reply) =&gt; {
                  console.log(reply);
                });
              });
          })
          .catch(err =&gt; {
            console.log("Skipped Mongodb");
          });
  
  </code></pre>