0

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.

5
  • You would typically use local variables in a closure that would be accessible inside that closure only (where the event handlers are). For more specific information on how to do this, please show your actual nodejs code. Commented Jul 10, 2020 at 8:22
  • @jfriend00: I know how closures work, but I don't want the user of my library/framework to be passing local variables around, that's what I'm trying to avoid. There's no code at the moment, but I can mock something up if this helps. Commented Jul 10, 2020 at 9:10
  • @jfriend00: there, I added some code. Commented Jul 10, 2020 at 9:14
  • Because of the event-driven, asynchronous nature of Javascript and how it runs all your code in one thread (except for WorkerThreads which is a different conversation entirely), the concept of thread locals doesn't really apply here. All your code runs in the same thread anyway so even if there were thread locals, they wouldn't help you. That's why you use closures and local variables or make the functions be methods of a common object and store the common data in that object or pass along a context object. Commented Jul 10, 2020 at 16:21
  • @jfriend00: that's not the only way. There's cls-hooked and async hooks, that don't depend on local variables. Commented Jul 10, 2020 at 16:57

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just keep in mind that is an experimental feature and there is no guarantee that the current design or API stays forever.
1

The one you are looking for is AsyncLocalStorage. Introduced in Node.js 12+ (stable in later versions), part of the async_hooks module. You can read more from here Even cls-hooked mentioned above internally uses this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.