Async Hooks In Nodejs
Posted By : Sakshi Gadia | 24-Jan-2018
Async_hooks makes it easier to trace resources easily. It is an experimental API came with node version 8. Async_hooks module provides an API to register its callbacks tracking the lifetime of resources which was created inside any Node.js application.
The advantage of Async_hooks:
Async_hooks API has all the information of the life of all handle objects. So that you know about all the objects and what all the changes its going through in its lifetime.
Installation
It can be accessed by:
const async_hooks = require('async_hooks');
For every async operation, create hooks function registers function to be called for a different lifetime.
init(), before(), after(), destroy() these callbacks are optional and called for different lifetime events of each async operation.
const hooks = { init: init before: before, after: after, destroy: destroy } const asyncHook = asyncHooks.createHook(hooks)
To enable the callbacks you have to use:
asyncHook.enable();
To disable the callbacks you have to use:
asyncHook.disable();
The init(async, type, triggered, resource) allows you to access the current resource, and look into what caused it to trigger.
function init(asyncId, type, triggerId){ const exec_id = asyncHooks.executionAsyncId(); fs.writeSync( 1, `${type}(${asyncId}): trigger: ${triggerId} execution: ${exec_id}\n`); }
- In the above function
asyncId is a unique Id for every async resource. - type identify what type of resource will cause init function to be called.
- triggered the new resource to initialize.
- a resource is an object that represents the actual async resource that has been initialized.
- executionAsyncId() specify the value of the current execution context.
let indent = 0; async_hooks.createHook({ init(asyncId, type, triggerId) { const exec_id = async_hooks.executionAsyncId(); const indentStr = ' '.repeat(indent); fs.writeSync( 1, `${indentStr}${type}(${asyncId}):` + ` trigger: ${triggerId} execution: ${exec_id}\n`); }, before(asyncId) { const indentStr = ' '.repeat(indent); fs.writeSync(1, `${indentStr}before: ${asyncId}\n`); indent += 2; }, after(asyncId) { indent -= 2; const indentStr = ' '.repeat(indent); fs.writeSync(1, `${indentStr}after: ${asyncId}\n`); }, destroy(asyncId) { const indentStr = ' '.repeat(indent); fs.writeSync(1, `${indentStr}destroy: ${asyncId}\n`); }, }).enable(); require('net').createServer(() => {}).listen(3000, () => { // Let's wait 9ms before logging the server started. setTimeout(() => { console.log('async_hooks>>>', async_hooks.executionAsyncId()); }, 9); });
The before() callback is called before the said callback is executed.
As the callback specified in before is completed after() callback is called immediately.
The destroy() callback is called after the resource corresponding to
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sakshi Gadia
An experienced MEAN Stack developer having good knowledge in Nodejs, MongoDb. Apart from these in my spare time, I enjoy playing chess and ready to learn new technologies.