async-debug is a Node.js module that debugs your asynchronous operations, inspired by asyncio debug mode of python.
Currently, the only feature of this module is to listen for your asynchronous operations and log them to the console if they take too long to complete (considered "slow").
As async_hooks does not provide information about the execution context, this module cannot provide the detailed location of where the operation was started like Python's asyncio. I will try to find a way to do this in the future. If you have any ideas, please let me know.
npm install async-debug yarn add async-debug pnpm add async-debug
import { configure } from "async-debug"; const asyncDebugger = configure({ longTaskThreshold: 250, // 0.25 seconds, default is 300 (0.3 seconds) }) if (process.env.NODE_ENV === 'development') { asyncDebugger.enable(); } // Your code here // Output: // Executing asynchronous operation 15 using HTTPCLIENTREQUEST took 313.654224999249ms to complete.
And that's it! You can now debug your asynchronous operations.
If you want to setup different thresholds for different operations, you can pass object to the longTaskThreshold parameter like below.
configure({ longTaskThreshold: { HTTPCLIENTREQUEST: 300, TCPCONNECTWRAP: 100, FSREQCALLBACK: 150, // See https://nodejs.org/api/async_hooks.html#type } })
All options are described as JSDoc comments, but if I think it's necessary, I'll add them here later.