I am a beginner in Node Js. I am not able to figure out why the below code gives syntax error with catch()
.I have upgraded the Node JS V14. Any help will be appreciated here
JavaScript
x
14
14
1
async function demoPromise() {
2
try {
3
let message1 = await myFirstPromise;
4
let message2 = await helloPromise();
5
console.log(message2);
6
} catch ((error) => {
7
console.log("Error:" + error.message);
8
})
9
}
10
11
(async () => {
12
await demoPromise();
13
})();
14
Error:
JavaScript
1
13
13
1
nodejsmarkasyncAwait.js:7
2
} catch ((error) => {
3
^
4
5
SyntaxError: Unexpected token '('
6
at wrapSafe (internal/modules/cjs/loader.js:979:16)
7
at Module._compile (internal/modules/cjs/loader.js:1027:27)
8
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
9
at Module.load (internal/modules/cjs/loader.js:928:32)
10
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
11
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
12
at internal/main/run_main_module.js:17:47
13
Advertisement
Answer
catch
doesn’t expect a function, it’s just a special syntax to detect the error:
JavaScript
1
6
1
try {
2
3
} catch(error) {
4
5
}
6