Reputation: 287690
I'm experiment with the design of a framework but I'm new to NodeJS and its implementation of event based programming. In other environments I would use thread local variables for what I'm trying to do, is there something equivalent in NodeJS?
Obviously in NodeJS you rarely use threads, but events. What I'm searching for is a global variable that once something set it, will continue to have that value as callbacks are called from the original one that set, but not from others. Does that make sense?
What I'm trying to do is design a library/framework, so, I don't have any code at the moment, but for example, imagine there's a function like this:
async function doSomething() {
await connect()
await doSomethingElse()
await doSomethingElseAgain()
}
I want connect()
to store the connection on a thread local variable, so that doSomethingElse()
and doSomethingElseAgain()
use that same connection, even when there are two threads of doSomething()
running (but I know they are not threads, but events running one after another from both executions of doSomething()
).
I don't want to pass any parameters from connect()
to other functions. I know how to do that already, but breaks the design I'm trying to come up with.
Upvotes: 0
Views: 736
Reputation: 287690
I'm not 100% sure as I'm still exploring, but I believe I have found the solution and it's the package named cls-hooked. I'll post more if I find more one way or another.
Upvotes: 1