#607·session

Feature request, return promises

Author: SoviutCreated Aug 28, 2018Updated May 16, 2025
Labelssemver-major

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