-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Support continuation/async local storage #7010
Comments
Not sure I understand your feature request. Can you sketch the API? I noticed that you created the |
The API would be similar to
It could be a third-party module, it could be part of Deno. In any case, the primitives upon which the module would be based must be there: the In any case, having this library is a key enabling technology for use cases like the AOP one I referenced above. IMHO, @ry Can you weigh in here? |
I totally missed the news on AsyncLocalStorage. Looks very useful. I worry a lot about adding browser incompatible APIs. Is there any hope of doing AsyncLocalStorage in the browser? |
This comment has been minimized.
This comment has been minimized.
I don't think that's related AFAICT @caspervonb - it's just a typical kv storage not a "green thread local storage" or however you want to describe AsyncLocalStorage. The "LocalStoarge" part of "AsyncLocalStorage" is very confusing. They are rather distinct APIs and shouldn't be using the same name. |
This comment has been minimized.
This comment has been minimized.
@ry I believe there is an initiative to revive zones but it might take a while before going there. |
@ry Yeah, I'm used to the Java name @vdeturckheim I'd ask you to not wait until |
@ry https://www.npmjs.com/package/zone.js is for browser.
Another issue about this is #5638 . It seems you didn't participate in that issue, so I @ you. |
Zones are dead as a TC39 proposal. There maybe an initial to revive it, but if anything it will be a cut down version of what was originally proposed, and therefore totally incompatible with what currently exists. Therefore there really isn't anything suitable that is standards based we can consider. |
There is a PR #8209 , but it's not for browser. |
Is there any update on this feature? |
No. As stated clearly above, there is not a web standard that would introduce this, and it depends upon a withdrawn TC39 proposal. Given that Node.js Just commenting "any updates" is annoying, because it requires the maintainers to check out and see if there was actually some important information that was missed. If you want to voice support for something, add your 👍 to the top of the issue. |
Node.js const storage = new AsyncLocalStorage();
function logWithId(msg) {
const id = storage.getStore();
console.log(`${id !== undefined ? id : '-'}:`, msg);
}
const result = await storage.run(crypto.randomUUID(), async () => {
logWithId('start');
await new Promise((resolve) => {
setTimeout(() => {
logWithId('finish');
resolve();
}, 200);
});
return "ok";
});
// result === "ok"
// storage.getStore() === undefined |
There is an async context proposal but it's pre Stage 1 https://github.com/legendecas/proposal-async-context It also appears that Cloudflare will end up shipping a similar API in workers |
I just opened tc39/proposal-async-context#17 over at the If you have Docker installed, you can
I would encourage anyone who has commented here to copy/paste this code into the REPL: const c = new AsyncContext();
c.run(123, async () => {
console.log("before await", c.get());
const awaitResult = await new Promise(resolve => {
setTimeout(() => resolve(c.get() + 111), 20);
});
console.log("after await", c.get(), { awaitResult });
}) Please note, I am not making any recommendation that If you think I might have messed something up with my changes to Deno, there's also a Please let me know what you think! |
FYI a polyfill for |
@bartlomieju |
You can use it by importing it directly from
|
@bartlomieju Is it possible to get support for async_hooks.executionAsyncResource() ? I found in DataDog/dd-trace-js#1892 (comment) |
@yoshixmk as it stands we don't plan to add support for that API. Currently we only want to support |
This issue is basically requesting the
deno
equivalent of https://www.npmjs.com/package/@northscaler/continuation-local-storage or https://nodejs.org/dist/latest-v14.x/docs/api/async_hooks.html#async_hooks_class_asynclocalstorage, with the additional feature as described at nodejs/node#32062 (comment).Without this, patterns like aspect-oriented programming (AOP) would be extremely difficult to implement. Here's an example of an AOP implementation based on
@northscaler/aspectify
that depends on continuation local storage to pull off its task: https://www.npmjs.com/package/@northscaler/securable-trait#security-via-aspect-oriented-programming-aopThe text was updated successfully, but these errors were encountered: