4 ms·
I recently stumbled upon something in JS (specifically NodeJS and a few other runtimes) that might be of interest here: AsyncLocalStorage (https://nodejs.org/ap
by kristiandupont 3y ago
I recently stumbled upon something in JS (specifically NodeJS and a few other runtimes) that might be of interest here: AsyncLocalStorage (https://nodejs.org/api/async_context.html https://nodejs.org/api/async_context.html). It allows for storing data specific to the current asynchronous context. I've been using it primarily for adding logging prefixes and managing authentication in Postgres with Row-Level Security (RLS).
Essentially, it offers a server-side alternative to "prop drilling," where you repetitively pass the same parameter through a chain of function calls.
- klysm 3y agoC# has the same thing and I use it for the exact same things
- namtab00 3y agowait, what? can you point to a feature name / doc / example?
- texuf 3y ago[dead]
- dtech 3y agoThis is extremely similar to thread local, a feature in many languages including C#.
- jcparkyn 3y agoI'm pretty sure they were talking about AsyncLocal<T>, which is not quite the same as ThreadLocal (values are persisted even when an async flow transitions across).
- klysm 3y agoYes it’s different but conceptually similar in the sense of ambient execution context. I prefer implicit variables like scala has but it gets the job done in C#.
- debugnik 3y agoThey mean AsyncLocal<T>, which ties its storage to the current thread-local ExecutionContext. When await schedules a continuation, the ExecutionContext flows with it and is set before resuming, so the AsyncLocal values flow through async methods even if the task switches threads.
- damagednoob 3y agoThis type of problem seems to be the exact reason why Object Orientated programming was developed in the first place. Whenever I'm repetitively passing arguments from one method to another, I usually find an object hiding in plain sight.