Home > front end >  Unhandled Promise Rejection Warning Occured
Unhandled Promise Rejection Warning Occured

Time:10-17

I want to create Product Categories , so i handle it in productController class database call in productCatService class. I added JS promise to this . now it gives following error.

productCatController.js

module.exports.createProductCat = async (request, response)=> {
 

        let result = await productCatService.createProductCat(productCatData);
      

        if (result) {
            responseService.successWithData(response, "Product Category Created");
        } else {
            responseService.errorWithMessage(response, result);
        }
   

}

productCatService.js

module.exports.createProductCat = (productCatData) => {


    let productCat = {
        name: productCatData.name,
        desc: productCatData.desc,
        count:productCatData.count,
        status : productCatData.status
    };


    return new Promise((resolve,reject)=>{
        ProductCategory.create(productCat).then(result => {
           resolve(true);
        }).catch(error => {
          reject(false)
        })
    });


}

Error

(node:18808) UnhandledPromiseRejectionWarning: false
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18808) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a p
romise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.
html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:18808) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a no
n-zero exit code.

CodePudding user response:

Never use await without try/catch. You don't have to try/catch every await, but somewhere down the call stack there needs to be a try/catch block.

You don't need try/catch here, just return the promise from ProductCategory.create()...

// productCatService.js
module.exports.createProductCat = (productCatData) => ProductCategory.create({
    name: productCatData.name,
    desc: productCatData.desc,
    count: productCatData.count,
    status: productCatData.status
});

...but you definitely need try/catch here, as this function is the bottom of the stack for this operation, and it is responsible for signifying overall success or failure to the caller.

// productCatController.js
module.exports.createProductCat = async (request, response) => {
    try {
        await productCatService.createProductCat(productCatData);
        responseService.successWithData(response, "Product Category Created");
    } catch (err) {
        responseService.errorWithMessage(response, err);
    }
}

Also don't use new Promise() for operations that already are promises. Keep using the promise you already have. Wrapping new Promise() around existing promises is a source of useless bloat, and it can introduce subtle bugs. Avoid.

  • Related