1

I'm having trouble understanding this piece of Javascript code:

var _ref3 = (0, (_asyncToGenerator2 || _load_asyncToGenerator()).default)(function* () {

if (actualArgs.length === 0) {
  const sandbox = yield getValidSandbox(curDir);
  // It's just a status command. Print the command that would be
  // used to setup the environment along with status of
  // the build processes, staleness, package validity etc.
  let envForThisPackageScripts = PackageEnvironment.calculateEnvironment(sandbox, sandbox.packageInfo, { useLooseEnvironment: true });
  console.log(PackageEnvironment.printEnvironment(envForThisPackageScripts));
} else {
  let builtInCommandName = actualArgs[0];
  let builtInCommand = builtInCommands[builtInCommandName];
  if (builtInCommand) {
    builtInCommand(curDir, ...process.argv.slice(3));
  } else {
    console.error(`unknown command: ${builtInCommandName}`);
  }
}
});

What exactly is _ref3 ? a function ? a tuple ? I'm confused

Attilah
  • 17,632
  • 38
  • 139
  • 202
  • 3
    what does `typeof _ref3` tell you? – Jamiec Mar 21 '17 at 09:19
  • @Jamiec, `typeof _ref3` says that it's a function. Thanks for the trick. – Attilah Mar 21 '17 at 09:22
  • Function with asterisk is a generator: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function* – Alex Slipknot Mar 21 '17 at 09:23
  • It depends on what value does `default` return – hindmost Mar 21 '17 at 09:24
  • In `(a,b)` expression, last value will be returned. So it will be either `(_asyncToGenerator2 || _load_asyncToGenerator()).default`. So final value will be the output of `.default` – Rajesh Mar 21 '17 at 09:24
  • 1
    You don't need to understand this code, any more than you need to understand the assembly code generated by a C compiler, unless you are studying transpilers. –  Mar 21 '17 at 09:56

1 Answers1

1

I would not like to read your code for you but i think, with a little help you could understand this code your self. I guess you need help with the various new syntax being used in the code above. I'll try to note down those so that you can understand all of this code yourself.

(0, (_asyncToGenerator2 || _load_asyncToGenerator()).default)(function*{})

This line basically is similar to

(0,x)(function*{}) 

where x is a function which takes a generator function as an argument.

Whenever you have a line of the form (x,y) it will always return the last value. So in the case of(x,y) it will return y. If its (0,x) it will return x. Thus in code which you posted, the first line will return (_asyncToGenerator2 || _load_asyncToGenerator()).default.

You could now translate the code to

((_asyncToGenerator2 || _load_asyncToGenerator()).default)(function* {})

This means that above code will return a function which takes a generator as argument

If you need more information on generator you could go here The generator function has attributes like yield. They are pretty useful especially to handle asynchronous operations. It streamlines your code and makes it easy to read. To get more information what yield means, you could go here and here

You could also see some lines like these in the code.

builtInCommand(curDir, ...process.argv.slice(3));

This is basically spread operators being used. Spread operators basically allow an expression to be expanded in places where multiple arguments are expected. You could go here to know more about spread operators.

Hope you will be able to read the above code yourself after you understand the concepts.

prajnavantha
  • 1,111
  • 13
  • 17