expressjs / expressjs/session

Feature request, return promises

Open
#607 5 comments 34 reactions 0 assignees View on GitHub
semver-major
Dominant language
JavaScript
Stars
6.4k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

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')

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.