Node: Syntax Error when using or and arrow function containing expression

Created on 13 Jun 2016  路  2Comments  路  Source: nodejs/node

  • Version: v6.2.1
  • Platform: Mac OS X 10.11.5
  • Subsystem: V8

It seems when preforming the following assignment V8 trips over the arrow function on the right side of the or:

const opts = {};
const doThing = opts.doThing || (cb) => cb(null);
> node test.js
/test.js:2
const doThing = opts.doThing || (cb) => cb(null);
                    ^
SyntaxError: Unexpected token .
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3

The issue goes away if you wrap the arrow function with parens.

const opts = {};
const doThing = opts.doThing || ((cb) => cb(null));
> node test.js
V8 Engine invalid question

All 2 comments

It's the intended operator precedence, see this comment: https://github.com/nodejs/node/issues/6892#issuecomment-220569946

As @vsemozhetbyt mentioned, this is by design and is explained in that link.

Was this page helpful?
0 / 5 - 0 ratings