-1

Could please someone explain me the syntax from nodejs docs, I don't understand the line:
(res) => {

TGrif
  • 4,873
  • 8
  • 27
  • 44
galina
  • 1

2 Answers2

1

(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

Chris Haugen
  • 805
  • 1
  • 7
  • 22
1

(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.

Dmitri Pavlutin
  • 14,964
  • 6
  • 34
  • 37