Node: Throws wrong error while using async/await as IIFE

Created on 18 Mar 2018  路  3Comments  路  Source: nodejs/node

  • Version: 9.8.0
  • Platform: Mac OS Sierra
  • Subsystem:

The below code throw Reference error when executed via Node.js

'use strict';

async function fetchConfig(opts) {
  return opts;
}

(async function test() {
  let err, config;
  const opts = { hi: 1}
  [err, config] = await fetchConfig(opts);
  console.log(config);
})()
(node:77719) UnhandledPromiseRejectionWarning: ReferenceError: opts is not defined
    at test (/Users/ecom-sathish.kumar/Desktop/Personal/TryOut/hapi-setup/server.js:10:37)
    at Object.<anonymous> (/Users/ecom-sathish.kumar/Desktop/Personal/TryOut/hapi-setup/server.js:12:3)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)
    at Function.Module.runMain (module.js:690:10)
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:666:3
(node:77719) 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(). (rejection id: 1)
(node:77719) [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.



md5-101d4b8ec7e7b6bdd91f8a4a1fc7f505



(node:77782) UnhandledPromiseRejectionWarning: TypeError: (intermediate value) is not iterable
at test (/Users/ecom-sathish.kumar/Desktop/Personal/TryOut/hapi-setup/server.js:10:19)
at
at process._tickCallback (internal/process/next_tick.js:118:7)
at Function.Module.runMain (module.js:692:11)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:666:3
(node:77782) 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(). (rejection id: 1)
(node:77782) [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.
```
As you can see I was expecting the above error instead of that I am getting Reference error. Can someone clarify what causes this issue?

Note: I have tried removing 'use strict' also.

Most helpful comment

Without semicolon, your code is parsed this way:

  const opts = { hi: 1}[err, config] = await fetchConfig(opts);

i.e.

const opts = { hi: 1}[undefined, undefined] = await fetchConfig(opts);

i.e.

const opts = { hi: 1}[undefined] = await fetchConfig(opts);

i.e.

const opts = undefined = await fetchConfig(opts); // sort of

i.e.

const opts = await fetchConfig(opts);

and here opts is referenced before it is defined which causes the ReferenceError.

All 3 comments

Without semicolon, your code is parsed this way:

  const opts = { hi: 1}[err, config] = await fetchConfig(opts);

i.e.

const opts = { hi: 1}[undefined, undefined] = await fetchConfig(opts);

i.e.

const opts = { hi: 1}[undefined] = await fetchConfig(opts);

i.e.

const opts = undefined = await fetchConfig(opts); // sort of

i.e.

const opts = await fetchConfig(opts);

and here opts is referenced before it is defined which causes the ReferenceError.

@vsemozhetbyt 馃槄 I see semi-colon is must in javascript. Whoever said semi-colon is optional can see the wrath of v8 engine. Thanks

I am definitely asking the above question in the interview. To see how many of them answer. :) 馃憤

Was this page helpful?
0 / 5 - 0 ratings