bellard / quickjs

Public repository of the QuickJS Javascript Engine.
https://bellard.org/quickjs
Other
8.39k stars 873 forks source link

[error] Errors thrown by asynchronous functions are not handled #305

Open absop opened 5 months ago

absop commented 5 months ago

Test code

async function gen() {
    return undefined_var;
}

gen().then((r) => {
    console.log(`r = ${r}`);
});

Run the code with qjs:

$ qjs --module test-async-error.js

Run the code with node:

$ node test-async-error.js
(node:3508) UnhandledPromiseRejectionWarning: ReferenceError: undefined_var is not defined
    at gen (/home/absop/Learning/test-async-error.js:3:5)
    at Object.<anonymous> (/home/absop/Learning/test-async-error.js:6:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
(node:3508) 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(). 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:3508) [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.
saghul commented 4 months ago

I think this is the same as https://github.com/quickjs-ng/quickjs/issues/340

andy0130tw commented 4 months ago

I would like to point out that Node's handling of unhandled rejections and exit code depends on the version and the value passed with CLI option --unhandled-rejections. The original issue seems to address an old version that is no longer supported officially. As of Node 16, the default value has been throw (node#41184), and the old behavior is equivalent to it being warn.