Feature request, return promises
See original GitHub issueIn 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:
- Created 5 years ago
- Reactions:29
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There are 2 ways:
Many popular libraries follow the second way, e.g.
fs-extra
orioredis
, so they support both callback and promise APIs. And now as I seethis
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?
It’d be nice to have promises returned elsewhere where callbacks are used as well, e.g.,
destroy
.