question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't force arrow function body on same line

See original GitHub issue

I created a function like this:

const promiseFromCallback = fn => 
  new Promise((resolve, reject) => 
    fn((err, result) => {
      if (err) return reject(err);
      return resolve(result);
    })
  );

This seems pretty clear to me with all the arrow function bodies moved to a new line. Prettier formats that function to this:

const promiseFromCallback = fn => new Promise((resolve, reject) => fn((
  err,
  result
) => {
  if (err) return reject(err);
  return resolve(result);
}));

At least to me that is a lot less readable than the first one. This formatting seems to be related to always moving the body of an arrow function to the same line as the arrow. I’m not sure this creates well formatted code.

I also added that code to prettier.github.io

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
vjeuxcommented, Mar 2, 2017

#847 makes it look:

const promiseFromCallback = fn =>
  new Promise((resolve, reject) => fn((err, result) => {
    if (err) return reject(err);
    return resolve(result);
  }));

instead. It’s not the original one but looks way less broken than what is currently in prettier.

2reactions
vjeuxcommented, Feb 21, 2017

Thanks for the report! This is not intended to be printed like this. #674 and #680 are attempts at fixing the problem but we’re not 100% sure what the root cause is.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does arrow functions behave when the body is a single ...
I wanted to know when arrow functions have a single line, the curly braces and the return keyword can be omitted, it implicitly...
Read more >
arrow-body-style - ESLint - Pluggable JavaScript Linter
"never" enforces no braces around the function body (constrains arrow functions to the role of returning an expression). The second one is an...
Read more >
ES6 Arrow Functions | What Not To Do | by Jacob Worrel
Arrow functions do not bind their own 'this' or 'arguments'. One of the great things about arrow functions is that they do not...
Read more >
Arrow Function JavaScript Tutorial – How to Declare a JS ...
If you have a one-line arrow function (like the example above), then the value is implicitly returned. So you can omit the return...
Read more >
Arrow function expressions - JavaScript - MDN Web Docs
Arrow functions don't have their own bindings to this , arguments , or ... If the body has additional lines of processing, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found