{"id":11414,"date":"2022-01-18T10:00:00","date_gmt":"2022-01-18T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11414"},"modified":"2022-01-17T20:00:31","modified_gmt":"2022-01-17T11:00:31","slug":"4-mistakes-that-should-be-careful-in-nodejs","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11414","title":{"rendered":"4 mistakes that should be careful in NodeJS"},"content":{"rendered":"\n<p>Today, I would like to share about four common mistakes that should be careful when develop with NodeJS. Let&#8217;s take a look.<\/p>\n\n\n\n<h3><strong>Blocking the Event Loop<\/strong><\/h3>\n\n\n\n<p>Since Node JS comes with a single-threaded environment, no two parts of a Node JS application can run parallelly.<\/p>\n\n\n\n<p>So, If the event loop is blocked, it causes blocking everything.<\/p>\n\n\n\n<p>The solution is to use open-source modules like StrongOps.<\/p>\n\n\n\n<p>StrongOps can identify and fix any delays in the event loop and prevent from blocking the event loop.<\/p>\n\n\n\n<h3><strong>Trying to Execute Multiple Callbacks<\/strong><\/h3>\n\n\n\n<p>Callbacks is used to allow asynchronous elements to communicate with each other.<\/p>\n\n\n\n<p>However, executing the same callbacks or multiple callbacks all at once can cause interface freeze.<\/p>\n\n\n\n<p>Therefore, it\u2019s important to ensure you\u2019re not continuously pressing for callbacks multiple times.<\/p>\n\n\n\n<p>There are two ways to avoid that.<\/p>\n\n\n\n<p>First, you can wrap the callback so you\u2019ll be notified each time a callback is executed twice.<\/p>\n\n\n\n<p>The second solution is to add a return before the callback to prevent invoking the same callback more than once.<\/p>\n\n\n\n<h3><strong>Callback Hell<\/strong><\/h3>\n\n\n\n<p>Callback Hell means &nbsp;multiple nested callbacks that make nesting callbacks difficult and impossible to read &amp; debug the written code.<\/p>\n\n\n\n<p>There are many ways to keep the code easy-to-read and debug such as using promises.<\/p>\n\n\n\n<p>A promise is basically a value returned to confirm the processing done by an asynchronous function. In simple terms, promises can be nested to make complex code look clear and easier to maintain.<\/p>\n\n\n\n<p>Another way is to use the async module that provides a straight forward, powerful functions to work with asynchronous JavaScript.<\/p>\n\n\n\n<h3><strong>Using Console.log for Debugging<\/strong><\/h3>\n\n\n\n<p>Console.log in the NodeJS is to print almost anything to the console. When Console.log is used to debug, it restarts the server and slows down the performance of the application.<\/p>\n\n\n\n<p>We can\u2019t use console.log every time. So instead of console.log, we can use the Debug Library.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>This is all for now.<\/p>\n\n\n\n<p>Hope you enjoy that.<\/p>\n\n\n\n<p>By Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11414\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11414\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"4 mistakes that should be careful in NodeJS\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11414\" data-text=\"4 mistakes that should be careful in NodeJS\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11414\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11414\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today, I would like to share about four common mistakes that should be careful when develop with NodeJS. Let&#038;# [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11414"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=11414"}],"version-history":[{"count":4,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11414\/revisions"}],"predecessor-version":[{"id":11420,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11414\/revisions\/11420"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}