UnhandledPromiseRejection despite catching expection

In the following code

// file: main.js
    class A {
    
        async start() {
            
            throw 'error';
        }
    }
    
    module.exports = A;



// file index.js

    var r = require('./main.js');
    
    let v = new r();
    
    try {
        v.start();
    } catch (e) {
        console.error(e);
    }

I am new to Javascript and Node.js, Node.js throws UnhandledPromiseRejection when I am clearly catching the exception, why does it happen ?

1 thought on “UnhandledPromiseRejection despite catching expection”

  1. This error originated either by throwing inside of an async function without a catch block

    To see the reason:

    node –trace-warnings index.js

    Reply

Leave a Comment