0

I don't even know what to call it so not sure how to ask the question properly. What is the operator and what is it doing? I'm assuming its just passing a function expression as a callback, but the syntax is new to me.

(err) =>

fs.writeFile('message.txt', 'Hello Node.js', (err) => {
  if (err) throw err;
  console.log('It\'s saved!');
});
Pompey Magnus
  • 2,191
  • 5
  • 27
  • 40
  • ES5 new arrow function syntax check it here https://babeljs.io/repl/ – Ilya Novojilov May 22 '16 at 18:34
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Arrow_functions – Sirko May 22 '16 at 18:35
  • You can omit braces about `err` also: `err => console.error(err)` – vp_arth May 22 '16 at 18:37
  • @IlyaNovojilov ES6, not ES5. –  May 22 '16 at 18:49
  • 1
    I strongly suggest you read up on ES6 features, since you are likely to encounter them more and more, including not only arrow functions, but also template strings and shorthand object literals to name just a few. –  May 22 '16 at 18:50

2 Answers2

5

It is a so-called arrow function. It is a short form for defining function expressions. Hence,

fs.writeFile('message.txt', 'Hello Node.js', (err) => {
  if (err) throw err;
  console.log('It\'s saved!');
});

is basically similar to:

fs.writeFile('message.txt', 'Hello Node.js', function (err) {
  if (err) throw err;
  console.log('It\'s saved!');
});

Anyway, there is big difference bitween the syntaxes: While function creates a new scope for this, an arrow function re-uses the outer scope. Hence, in a callback defined using the function keyword you may need something such as

const that = this;

to preserve the outer scope, using an arrow function you don't need that. Please note that this also means that you are not able to use bind with an arrow function (i.e., it's not possible to redefine this for an arrow function (okay, to be true, you can use it, but the first parameter won't have any effect)).

Apart from that, please note that you can omit the parentheses around the parameter if there is only one. Hence, instead of

fs.writeFile('message.txt', 'Hello Node.js', (err) => {
  if (err) throw err;
  console.log('It\'s saved!');
});

you may also write:

fs.writeFile('message.txt', 'Hello Node.js', err => {
  if (err) throw err;
  console.log('It\'s saved!');
});

Anyway, this only works if there is only one parameter.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
0

This is ES6's arrow function (or lambda) syntax. Check out this MDN article.

It is indeed a callback, that will accept an argument named err.

In your case it is semantically equivalent to the following :

function(err) {
  if (err) throw err;
  console.log('It\'s saved!');
}
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • 3
    It is semantically **not** equivalent. Arrow functions deal differently with `this`. – Golo Roden May 22 '16 at 18:37
  • ...and function-scoped variables don't scope to them. – metarmask May 22 '16 at 18:37
  • 1
    Woops, you're right. I won't go into the details but have edited my answer to specify it is only equivalent in this specific case. – Aaron May 22 '16 at 18:39
  • 1
    @metarmask What do you mean by *function-scoped variables don't scope to them*? –  May 22 '16 at 18:51
  • @torazaburo `var flag = true;` creates a function-scoped variable. Those variables can only be accessed from within the function they are in. `let flag = false;` creates a block-scoped variable which can only be accessed from the block it is declared in (or blocks within that block). Arrow functions create a block. – metarmask May 22 '16 at 19:09