Is it possible to have “thread” local variables in Node?

前端 未结 3 1060
萌比男神i
萌比男神i 2021-02-05 08:16

I would like to store a variable that is shared between all stack frames (top down) in a call chain. Much like ThreadLocal in Java or C#.

I have found https://github.com

3条回答
  •  长情又很酷
    2021-02-05 08:53

    Now that more than a year has passed since I originally asked this question, it finally looks like we have a working solution in the form of Async Hooks in Node.js 8.

    https://nodejs.org/api/async_hooks.html

    The API is still experimental, but even then it looks like there is already a fork of Continuation-Local-Storage that uses this new API internally.

    https://www.npmjs.com/package/cls-hooked

提交回复
热议问题