othiym23 / node-continuation-local-storage

implementation of https://github.com/joyent/node/issues/5243
BSD 2-Clause "Simplified" License
1.13k stars 207 forks source link

Pros / cons on just using cookies? #136

Open qodesmith opened 5 years ago

qodesmith commented 5 years ago

Forgive me for my questions if they seem obvious. I've recently discovered this package since we're using it in a project here on the job. From a high level, it seems that this package can set information in one module that can later be retrieved in a completely different module.

I'm sure this package is doing something special or addressing a particular use-case, what with all the talk about threading and Node processes, but the lightbulb isn't turning on for me. Why not just use cookies instead of this package? Cookies are accessible in every request made to the server. Just trying to understand the particular use-case of this package. Thanks in advance!

watson commented 5 years ago

While this module can just as well be used in situations where the Node.js process isn't an HTTP server, let me just quickly answer your question in case it was always an HTTP server: Within a single request to the Node.js HTTP server the Node process might be performing several async operations:

In all of these cases, whenever the async operation finishes, the JavaScript code executing on the stack has no knowledge of how the process got to that point. Every thing up until the callback was called is "lost". Notice how this can happen hundreds of times within the same HTTP request, and so there's no time to store a cookie in the users browser. So if you want to store a state between each of these async operations, you need to either:

The former approach is a bit tedious, so many people like the latter approach better.