Could please someone explain me the syntax from nodejs docs,
I don't understand the line:
(res) => {
-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Sterling Archer Jan 12 '17 at 17:37
2 Answers
(res) => {}
is a fat arrow function. Similar to function(res) {}
with one big difference, this
is scoped differently.
in ES6 the fat arrow function was introduce and pretty much does two things to my understanding:
1) It makes the syntax more concise, less stuff to type
2) It lets the this
reference stay as a reference to the function's parent.
Read up more about lambda unctions here

- 825
- 1
- 7
- 22
(res) => { ... }
is the ES6/ES2015 syntax for anonymous functions. It is called arrow functions.
e.g.
var add = function (x, y) { return x + y; }
...can now be written as:
var add = (x, y) => { return x + y; }
...but if it has just one line and that line is a return statement, you can write it as:
var add = (x, y) => x + y
These fat arrow functions preserve the lexical scope of this
, so there are times when NOT to use arrow functions though. Typically, these are situations when you are declaring a function that depends on the this
reference to be something other than the this
context that you are declaring the function in.

- 18,122
- 8
- 37
- 41

- 11
- 1