Questions tagged [async-hooks]

The async_hooks module provides an API to register callbacks tracking the lifetime of asynchronous resources created inside a Node.js application. It is currently an experimental API.

36 questions
8
votes
4 answers

How to use AsyncLocalStorage for an Observable?

I'd like to use use AsyncLocalStorage in a NestJs Interceptor: export interface CallHandler { handle(): Observable; } export interface NestInterceptor { intercept(context: ExecutionContext, next:…
TmTron
  • 17,012
  • 10
  • 94
  • 142
5
votes
1 answer

Does Deno have a Way to Track Async Contexts?

Speaking broadly, in NodeJS a program creates "an async context" whenever code runs that creates a callback-to-be-run-later via the Event Loop or the microtask queue. Network requests, setTimeout callbacks, Promises, etc. In modern versions of…
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
5
votes
1 answer

node async_hooks destroy lifecycle event is not called for all resources

I've been exploring the async_hooks API for the purposes of tracking state across asynchronous events. I'm finding that the destroy callback isn't always called for each corresponding init callbacks. Here's a simple repro: const asyncHooks =…
ColinE
  • 68,894
  • 15
  • 164
  • 232
4
votes
1 answer

snowflake-sdk: Module not found: Error: Can't resolve 'async_hooks' in 'C:\projectname\node_modules\vm2\lib'

I am trying to integrate Snowflake in my Cypress tests but it keeps on throwing error while compiling. Error: Error: Webpack Compilation Error ./node_modules/vm2/lib/resolver-compat.js Module not found: Error: Can't resolve 'async_hooks' in…
3
votes
0 answers

AsyncLocalStorage loose context in mongoose hooks POST save

How to do to maintain the same context in all flow ? Why the AsyncHooks context is loose inside the Schema.post('save') but not in Schema.pre('save')? Please, look the testes-mongoose branch and execute yarn example:mongoose. GIT HUB…
3
votes
0 answers

Performance implications of using Async Hooks

I'm the author of a Node.js library and I want to use Async Hooks to improve DX. But I'm worried about performance. I've read stories saying that everything is fine, as well as stories where performance was a blocker. In theory, asynchronicity…
brillout
  • 7,804
  • 11
  • 72
  • 84
3
votes
1 answer

Nodejs AsyncLocalStorage getStore() return undefined

I read this post AsyncLocalStorage for Easy Context Passing in Node.js I try to get logId in my logs, but i cant, beacause asyncLocalStorage.getStore() return undefined. It seems that context was lost inside MyLogger class. How to solve it? Here is…
Masquitos
  • 554
  • 4
  • 22
3
votes
0 answers

cls-hooked wrapped into a shared library doesn't work

I'm working on request tracing in my node.js project (monorepo) and found out something called "cls-hooked" which uses async hooks. So I simply wrapped cls-hooked functionalities into adapter that I want to put into a shared library that I use in…
3
votes
2 answers

Why does my use of the async hooks API cause an abnormal termination of Node.js?

I've broken Node.js!! I'm using the async hooks API and my code is making Node.js terminate abnormally. My question is: what is it about this code that makes Node.js terminate in this way and is there anything I can change in the code that fixes…
Ashley Davis
  • 9,896
  • 7
  • 69
  • 87
3
votes
1 answer

Error: Cannot find module 'async_hooks' in NodeJs

I am trying to run test code of Async Hooks from official node api documentation and i am getting error Error: Cannot find module 'async_hooks' in console. I already include const async_hooks = require('async_hooks'); at the top of my script. Here…
Jitendra
  • 3,135
  • 2
  • 26
  • 42
3
votes
2 answers

Tracking context with async_hooks

I'm trying to track context through the async stack using node async_hooks. It works for most cases, however I have found this use case that I can't think how to resolve: service.js: const asyncHooks = require('async_hooks'); class Service { …
DomA
  • 138
  • 1
  • 8
2
votes
0 answers

Nodejs Async_hooks Remove Context When i use await

Hi Everone I am using async_hook for request context console.log("xxx",hook.getRequestContext()) // undefined because of i am using promise before var hook = require('../../services/hooks.service'); const test11 = async (req,res)=>{ let…
2
votes
1 answer

Node.js async_hooks loss tracing

I have read the doc of nodejs async_hooks module and want have a try, this is my demo.mjs: import async_hooks, { executionAsyncId } from 'async_hooks'; import fs from 'fs'; import path from 'path'; // a async version of console.log function…
Vista Chyi
  • 75
  • 7
2
votes
1 answer

What is the difference between async await and async_hooks in Node.js

async_hooks were introduced as experimental in Node v8. Because the name is similar to ES2017 async it could appear that they may be in some way related. Are they? If so, in what way (complementary or competing)?
cmcculloh
  • 47,596
  • 40
  • 105
  • 130
2
votes
1 answer

Are there any npm modules that use async_hooks to retrieve async stack trace?

There is a new API in node 8 called 'async_hook' which I believe should make it possible for a module author to print async stack traces. I'm looking for something similar to chrome dev-tools async stack-trace implementation but for use from…
Ben
  • 977
  • 2
  • 11
  • 21
1
2 3