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.

Feature request, return promises

See original GitHub issue

In conjunction with the callbacks, it would be nice if session.save() returned a promise as well. This would make in-lining session operations with if blocks, try/catch blocks and loops a lot more straightforward since async/await could be used.

For example, I’m running into race conditions with redirecting happening too soon. So in situations where I need to set the session, like a “flash” message, I have to do this:

try {
  // some error prone operation
} catch(err) {
  flash('error', 'something went wrong')
}
req.session.save((err) => {
  res.redirect('/things')
})

You’ll notice session.save() is called regardless of whether the error occurs. I’m choosing to do that instead of duplicate the res.redirect() code into multiple places.

However, what I’d rather have is:

try {
  // some error prone operation
} catch(err) {
  flash('error', 'something went wrong')
  await req.session.save()
}
res.redirect('/things')

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:29
  • Comments:5

github_iconTop GitHub Comments

4reactions
RussCodercommented, Oct 10, 2019

There are 2 ways:

  1. Always return a promise, even if a callback is passed in.
  2. Return a promise if no callback was provided.

Many popular libraries follow the second way, e.g. fs-extra or ioredis, so they support both callback and promise APIs. And now as I see this is returned from all session methods. So, perhaps, the second way is better.

@dougwilson

If I create a pull request with this feature, will you accept it? And what way to choose?

3reactions
brettz9commented, Sep 15, 2019

It’d be nice to have promises returned elsewhere where callbacks are used as well, e.g., destroy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise - JavaScript - MDN Web Docs
Returns a new Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then method),...
Read more >
Why my promise based request returns Promise object on ...
It is impossible to return the data, because the data doesn't exist yet. Instead, you will need to return a promise. async functions ......
Read more >
Using JavaScript Promises - AWS Documentation
However, AWS.Request.promise immediately starts the service call and returns a promise that is either fulfilled with the response data property or rejected with ......
Read more >
Getting Started With Axios: A Popular Promise-Based HTTP ...
You have to check the status code in the response object in order to determine if the request was successful. On the other...
Read more >
25. Promises for asynchronous programming - Exploring JS
No inversion of control: similarly to synchronous code, Promise-based functions return results, they don't (directly) continue – and control – execution via ...
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