DEV Community

Discussion on: ES6, the arrow function

Collapse
 
ahmedtarek_32 profile image
Ahmed Tarek

I like arrow functions in the way that they deal with promises, writing a promise specifically is easier using an arrow function and in my opinion, the only place where it is a bit more readable and organized than the usual function keyword. I also like that if you have only one parameter, you can pass it on without any brackets and it would still work which makes for a good and more organized code structure.

// An example of using the package "inquirer" and getting data back
let myPromise = function () {
  return new Promise(function (resolve, reject) {
    inquirer.prompt(question).then(function (data) {
      resolve(data)
    })
  })
}
// With arrow functions
let myArrowPromise = () => new Promise(resolve => {
  inquirer.prompt(question).then(data => {
    resolve(data)
  })
})

I know that it might only be 2 lines of code difference but writing huge files of javascript using the function keyword can get quite messy specially having a function inside another and so on just bugs my head writing a promise with arrow functions almost feels like one function, arrow functions are definitely great!