0

I'm relatively new to AJAX and I don't understand how do arrow function works with promise
well the syntax I'm familiar with the following:

functionThatReturnsAPromise()
  .then(function(data) {
    // Do somthing with data 
  })
  .catch(function(err) {
    // Do something with error
  });

but I have come accross this code which based on an arrow function, I really don't understand it:

getActivityInformations(event) {
  return this.service.getEntityRep(a).then(list => {
    const myList = list ? list.length : 0;
    // some code
  });
}
kissu
  • 40,416
  • 14
  • 65
  • 133
chaf ch r
  • 39
  • 5
  • How promises work doesn't change depending on what syntax the `then` callback function is defined with. `list => {…}` is just the same as `function(list) {…}`. – Bergi Apr 03 '22 at 15:38
  • Arrow functions are not specific to promises. Have a look at the MDN reference for arrow functions to learn more about those: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Andreas Apr 03 '22 at 15:39
  • Nothing here is specific to promises. You will naturally have problems accessing correct this context in regular function, see https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback – Estus Flask Apr 03 '22 at 16:09

0 Answers0