Difference with async-local-storage
See original GitHub issueCan someone explain what is the difference between this library compared to https://github.com/vicanso/async-local-storage ? That library seems super simple.
I checked other solutions as we are having issues with destroying the namespace. Is destroying the namespace necessary on every request? Here is our middleware:
return async function(ctx, next) {
const namespace = continuationLocalStorage.createNamespace('session');
try{
await new Promise(namespace.bind(function(resolve, reject) {
namespace.set('request_id', ctx.request.header['x-request-id']);
next().then(resolve).catch(reject);
}));
} catch(error) {
throw error;
} finally {
continuationLocalStorage.destroyNamespace('session');
}
};
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Node.js 14 & AsyncLocalStorage: Share a context between ...
AsyncLocalStorage is used to share data between asynchronous calls. This can be considered as a global variable related to a particular ...
Read more >Asynchronous context tracking | Node.js v19.3.0 Documentation
It is similar to thread-local storage in other languages. The AsyncLocalStorage and AsyncResource classes are part of the node:async_hooks module: import { ...
Read more >What's Async Local Storage in Node.js v14? - freeCodeCamp
Introducing Async Local Storage – Storage for asynchronous tasks. Consider a web server like Apache running PHP for hosting a website. When PHP ......
Read more >Async local storage - AdonisJS
It is similar to thread-local storage in other languages." To simplify the explanation further, AsyncLocalStorage allows you to store a state when executing...
Read more >Logging with Pino and AsyncLocalStorage in Node.js
Proper logging can be the difference between a lousy dump of debug statements and a powerful debugging tool that helps you find bugs...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
At first glance and without comparing in detail the async_hook event state management of async-local-storage, I assume for the latest node 8 versions, it and cls-hooked would behave the same.
One clear difference is cls-hooked provides backward compatibility with node 6 & 7 using AsyncWrap when running in those versions. It also provides the same interface as node-continuation-local-storage to make it easier for devs to try.
As for destroying namespaces, its not required but I guess it depends how you’re using it. I’m not sure what framework your middleware example is from, but usually namespaces are created sparingly, such as a “sessions” namespace, while contexts are created on that namespace for things like requests and sessions.
Btw, I love the simple and elegant code of
async-local-storage
… it makes me want to rewritecls-hooked
… when days start to have 36 hours in them…@blacksonic Closing for now. Let me know if you have any issues.