title: catching thrown errors from callback function javascript
body: <p>I'm running some functions inside try-catch. I'm using fs.unlink to remove some files on request. but if there is not such a file I want to throw an error. but I can't catch the error. it always causes the app to crash</p>
  <p>this is my code:</p>
  <pre><code>  app.delete(&quot;/product&quot;, (req, res) =&gt; {
      const id = req.body.id;
  
      try {
        Product.findByPk(id)
          .then((product) =&gt; {
            if(product) {
              const images = JSON.parse(product.image);
  
              for(let i = 0; i &lt; images.length; i++) {
                fs.unlink(`public/images/${images[i]}`, (err) =&gt; {
                  if (err) throw new Error(err);
                });
              }
      
              product.destroy();
            }else {
              res.status(404).json({ msg: &quot;can't delete product with id: &quot; + id + &quot;. It doesn't exist in database.&quot;});
            }
          });
        res.status(200).json({ msg: &quot;Deleted product with id: &quot; + id });
      } catch (error) {      
        res.status(500).json({ msg: &quot;An error with code 'NW_DELETE_1' occured.&quot; });
        Log.error(&quot;ERROR is this: &quot; + error);
      }
  
    });
  </code></pre>
  <p>the Product.finByPk is from <code>Sequelize</code></p>
  <p>and Log object is from <code>bunyan</code> logger.</p>
  <p>can you please show me how can I handle every possible error in this block of code?
  I wan't to catch the error and log it to console using Log. I wan't to avoid programmer errors like non-existing file for deleting on fs.unlink.</p>
  <p>I'm new to programming. please note any mistake you find.</p>