Search code examples
javascriptnode.jspromisees6-promise

JS Promises - Why am I seeing the UnhandledPromiseRejectionWarning and DeprecationWarning?


I am getting to grips with promises and how to set them up, however I can't see why node thinks my promise is unhandled when it comes to errors... Can somebody please explain?

My simple code

    // Setting up the Promise
    function add(x, y) {
        return new Promise((response, reject) => {
            // Simple conditional (not perfect, but it just proves a point)
            if(x !== null && y !== null) {
                // I know I could have done 'response(x + y)', but I wanted 
                // to console.log the result also
                var calc = x + y
                response(calc)
                console.log('Calculation: ' + x + ' + ' + y + ' = ' + calc)
            } else {
                // My console does not throw this error?
                reject('One of the inputs was null')
            }
        })
    }
    
    // Function using the Promise
    function calc() {
        add(1, 3)
            .then(res => add(res, 3))
            .then(res => add(res, null))
            .then(res => console.log('Final result: '+res))
            .catch(err => {
                // This error is thrown in console
                throw new Error('Something went horribly wrong')
            })
    }
    
    // Run the code
    calc();

Update

I originally posted the 'reject' with a thrown error, of which I understand needs to be caught.

I also want to understand why the string in the 'reject' is not seen in my console?

Console output:

Calculation: 1 + 3 = 4
Calculation: 4 + 3 = 7
(node:61950) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Something went horribly wrong
(node:61950) [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.

Solution

  • In your function using the Promise :

    // Function using the Promise
    function calc() {
        add(1, 3)
            .then(res => add(res, 3))
            .then(res => add(res, null))
            .then(res => console.log('Final result: '+res))
            .catch(err => {
                // This error is thrown in console
        --->    throw new Error('Something went horribly wrong')
            })
    }
    

    The line maked with ---> throws an Error. That error isn't caught. Usually when you catch an error you want to do something with it. If you throw it back or throw another error, that throw should be caught.

    What I'd do with this is the following :

    // Function using the Promise
    function calc() {
        return add(1, 3)
            .then(res => add(res, 3))
            .then(res => add(res, null))
            .then(res => console.log('Final result: '+res))
            .catch(err => {
                // This error is thrown in console
                throw new Error('Something went horribly wrong')
            })
    }
    
    calc().catch(err => {
        console.log(error.message); // For example
    });