-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AsyncLocalStorage loses a context #2537
Comments
Thanks for the heads up @gireeshpunathil ! @Doc999tor so, you are entering the AsyncLocalStorage context outside of a transaction. You want to call The solution here is to change the first piece of code by something like app.use((req, res, next) => {
asyncLocalStorage.runSyncAndReturn({}, () => next());
});
app.use(asyncRouter.asyncRouter); and you should see what you want. Let me know if that works! |
Thanks @vdeturckheim |
I am not sure I understand your need here. In my example, I still add In term of async operation, the code running inside a middleware is not a child of the code that instanciated the middleware itself. AsyncLocalStrorage is a transactional API that spread a context through the event loop. |
If I wrap the express instantiating and the middleware use call, the async context is kept the same:
Is it because the |
I am not sure I get this one, do you mean the store you get inside middlewares execution is the one you instanciated when registering the middlewares? |
I tested it first place - this store is fully available in all the middlewares invoked inside the
|
Have you tried the same thing with By doing so, I belive the http.Server instance is created withing the context and therefore has access to the store. |
Sure, thus I did
Instead of creating a God-object stores all kinds of information ever being useful in the whole nodejs server, I wanted to create smaller separate stores for different kinds of nodejs services, to decouple logic |
There has been no activity on this issue for 3 years and it may no longer be relevant. It will be closed 1 month after the last non-automated comment. |
There has been no activity on this issue and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment. |
Docs say: In this example, the store is only available in the callback function and the functions called by
foo
. Outside ofrunSyncAndReturn
, callinggetStore
will returnundefined
.I need the store available in the scope of
asyncRouter
onlyHow should I persist context is the example below?
Thanks
Express
plain example:File: async-store.js
File: app.js
File async-router.js
The text was updated successfully, but these errors were encountered: