a blog for those who code

Friday 14 July 2017

What is Async Hooks in Node.js 8

In this post we will be discussing about Async Hooks in Node.js 8. Async Hooks API will allow the developers to track asynchronous requests and handles through their complete life-cycle. In our previous post we have discussed What is New in Node.js Version 8, in that Async Hooks is one of the noticeable feature added in the new version of Node.js. Now we will further discuss about Async Hooks and what will be its advantage.

According to Node.js Doc, Async Hooks module provides an API to register callbacks tracking the lifetime of asynchronous resources (i.e. an object with an associated callback) created inside a Node.js application. It can be accessed inside the application using the below code :

const async_hooks = require('async_hooks');

The advantage of using Async Hooks API is that the API emits events which has all the information about the life of all handle objects. This is important because then you know about all the objects and what all the changes its going through in its life time.

To start with we have to create a AsyncHook instance and then allow the callbacks to be called as shown below ;

const asyncHook = aync_hooks.createHook(callbacks)
// the callbacks to register and returns AsyncHook instance used for enabling and disabling hooks
// the callbacks used are init(), before(), after() and destroy() and these are all optional.
asyncHook.enable()
// it enables the callbacks for a given AsyncHook instance
asyncHook.disable()
// it disables the callbacks for a given AsyncHook instance

Still its a very early age for Async Hooks and its done on an experiment basis but its a very important feature for developers because now all the asynchronous requests can be tracked by the users.

Please like and share the CodingDefined.com blog if you find it useful and helpful.
{"cc_re__cc_re__cc_by_sa_3_0": ["t' id='ContactForm1_contact-form-submit' type='button' value='Send'/>\n<p></p>\n<div style='text-align: center; max-width: 222px; width: 100%'>\n<p class='contact-form-error-message' id='ContactForm1_contact-form-error-message'></p>\n<p class='contact-form-success-message' id='ContactForm1_contact-form-success-message'></p>\n</div>\n</form>\n</div>\n</div>\n<div class='clear'></div>\n</div><div class='widget HTML' data-version='1' id='HTML4'>\n<h2 class='title'>CC License</h2>\n<div class='widget-content'>\n<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://lh3-testonly.googleusercontent.com/blogger_img_proxy/AEn0k_sbG0nQxwBTUEk3v28xUfP8bU5H_PLqFNXGq1CXbbnOO1OKdAUSV0-cjIyXbDX7-QAKsXGOvPYnpZQPoEBpi3VbQ_Vl41wH3EmJUGTuL1C6HsCTZZdW=s0-d\"></a><br />This work is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/\">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>\n</div>\n<div class='clear'></div>\n</div></div>\n</div>\n<div id='lowerbar-w", "clear'></div>\n</div><div class='widget HTML' data-version='1' id='HTML4'>\n<h2 class='title'>CC License</h2>\n<div class='widget-content'>\n<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://lh3-testonly.googleusercontent.com/blogger_img_proxy/AEn0k_sbG0nQxwBTUEk3v28xUfP8bU5H_PLqFNXGq1CXbbnOO1OKdAUSV0-cjIyXbDX7-QAKsXGOvPYnpZQPoEBpi3VbQ_Vl41wH3EmJUGTuL1C6HsCTZZdW=s0-d\"></a><br />This work is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/\">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>\n</div>\n<div class='clear'></div>\n</div></div>\n</div>\n<div id='lowerbar-wrapper'>\n<div class='lowerbar ready-widget section' id='lowerbar2'><div class='widget PopularPosts' data-version='1' id='PopularPosts2'>\n<h2>Popular Posts in Last 7 Days</h2>\n<div class='widget-content popular-posts'>\n<ul>\n<li>\n<a href='https://www.codingdefined.com/2015/06/get-call-stack-information-in-nodejs.html'>Get Call Stack Information in NodeJS</a>\n</li"], "copyright__copyright__copyright": ["efore(), after() and destroy() and these are all optional.\nasyncHook.enable()\n// it enables the callbacks for a given AsyncHook instance\nasyncHook.disable()\n// it disables the callbacks for a given AsyncHook instance\n\nStill its a very early age for Async Hooks and its done on an experiment basis but its a very important feature for developers because now all the asynchronous requests can be tracked by the users.\n\nPlease like and share the CodingDefined.com blog if you find it useful and helpful.", "efore(), after() and destroy() and these are all optional.\nasyncHook.enable()\n// it enables the callbacks for a given AsyncHook instance\nasyncHook.disable()\n// it disables the callbacks for a given AsyncHook instance\n\nStill its a very early age for Async Hooks and its done on an experiment basis but its a very important feature for developers because now all the asynchronous requests can be tracked by the users.\n\nPlease like and share the CodingDefined.com blog if you find it useful and helpful.", "efore(), after() and destroy() and these are all optional.\nasyncHook.enable()\n// it enables the callbacks for a given AsyncHook instance\nasyncHook.disable()\n// it disables the callbacks for a given AsyncHook instance\n\nStill its a very early age for Async Hooks and its done on an experiment basis but its a very important feature for developers because now all the asynchronous requests can be tracked by the users.\n\nPlease like and share the CodingDefined.com blog if you find it useful and helpful.", "efore(), after() and destroy() and these are all optional.\nasyncHook.enable()\n// it enables the callbacks for a given AsyncHook instance\nasyncHook.disable()\n// it disables the callbacks for a given AsyncHook instance\n\nStill its a very early age for Async Hooks and its done on an experiment basis but its a very important feature for developers because now all the asynchronous requests can be tracked by the users.\n\nPlease like and share the CodingDefined.com blog if you find it useful and helpful."]}