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.

Best practices for promises?

See original GitHub issue

First of all, great job on this guide, I agree with it almost 100%. All of this should really be common knowledge, but I sure as hell didn’t know most of these things when I started, so kudos for writing it all down.

Now, since promises have been standardized and a lot of people have started using them, it would be nice to have a section on how to keep promise-based code readable. For instance, when chaining multiple promises together, there are at least two alternative ways to do it:

Option 1: anonymous functions

return doSomething(foo).then(function(result) {
  // ...
}).then(function(result2) {
  // ...
}).then(function(result3) {
  // ...
}).catch(function(error) {
  // ...
}).finally(function() {
  // ...
});

Option 2: named functions

function doThis(result) {
  // ...
}
function doThat(result) {
  // ...
}
function handleError(error) {
  // ...
}
function done() {
  // ...
}

return doSomething(foo)
  .then(doThis)
  .then(doThat)
  .catch(handleError)
  .finally(done);

The second way, while being somewhat more verbose, seems to be preferable, as the chain of tasks that the promise result travels through becomes more obvious.

I did an internal poll of our developers, and so far, option 2 seems to be ahead, although I have not heard from everyone yet. How does AirBnB deal with this, and what are your opinions?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:67
  • Comments:46 (1 by maintainers)

github_iconTop GitHub Comments

150reactions
getvegacommented, Mar 31, 2015

About best practices, seems like you all put the .then on the same line instead of a new line…

What do you think of

getPromise()
    .then(function(response) {
         return response.body;
    })
    .then(function(body) {
         return body.user;
    });

It seems a bit more compliant with leading dots in https://github.com/airbnb/javascript#whitespace

27reactions
chandanchcommented, Jan 22, 2018

Chaining promises is fine but the chain should not go beyond 2 max. 3

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for ES6 Promises - DEV Community ‍ ‍
Best Practices for ES6 Promises · Handle promise rejections · Keep it "linear" · util.promisify is your best friend · Avoid the sequential...
Read more >
JavaScript Best Practices — Promises | by John Au-Yeung
JavaScript Best Practices — Promises · Catching or Returning · We Should Return Something or Throw Errors · Consistent Parameter Names when ...
Read more >
Using promises - JavaScript - MDN Web Docs
Luckily we can wrap setTimeout in a promise. The best practice is to wrap the callback-accepting functions at the lowest possible level, ...
Read more >
Best Practices With JavaScript Promises - Better Programming
In this article, we'll look at best practices for using the latest JavaScript promise features. Converting Non-Promise Async Code to Promises.
Read more >
Best Practices for Using Promises in JS - 60devs
1.) Build you interfaces using Promises ; 2.) In nodejs, always use bluebird (or similar) as Promise implementation ; 3.) Don't define rejection ......
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