Skip to main content

Node.js async functions error handling

If you come from start of Node.js, you probably remember that the callback functions had always by convention a error as it's first parameter and the result as the second one.
Now days, most people use async functions instead of callbacks because they leave the code more clean and are usually easier to understand by someone that comes from programming in other languages.
In my opinion, a problem that exists when using async functions is the error handling. Usually people use try / catch blocks but there is a better way i will show you now.
You could have a wrapper function or handler function that always return a pattern like in callback functions, this is, a error as first and a result as second parameter.
Take a look at the gist below so you could understand what i'm talking about:

I created a function that validates if the user input is the number one (1). If it's valid, it returns the message "OK" and if it's not, it returns the error message saying that the user must type (1).
Simply download the gist and type in your terminal:
node async_error_handling.js {value}
where value must be a number. To try the positive result, replace "value" with 1 and to try the error result, replace "value" with any other value than 1.
Now you could use the asyncHandler to handle you async/await calls.
Hope you enjoy!

Comments

Popular posts from this blog

Connecting to a serial port on MAC OS

Sometimes you need to connect to the serial port of a device from your MAC OS computer. It's pretty simple, first you need to list all your serial ports using the command: ls /dev/tty* Then, find the device name you want to connect to and type the command: screen /dev/tty[DEVICE_NAME] , In the example above, if you want to connect to the tty.usbserial device, you will type in your terminal: screen /dev/tty.usbserial Type enter, wait some seconds and you will be connected to the device serial port!

Setup a new Vue.js application with Vue CLI

Nowadays, Vue.js is one of the simplest and fast growing web frameworks available for the community. It's also pretty simple to start a new project with the Vue CLI tool. First, you will need to install it on your computer. Ensure you have npm installed and simply type the following in your terminal: npm install @vue/cli or, if you have yarn : yarn global add @vue/cli Now, please confirm you have it installed typing: vue -V Once you have properly installed the tool, the only thing you need to start a new Vue.js application is to choose a folder from your terminal and type the command: vue create your-app-name You will see the magic happening, follow the steps and now you have a basic Vue.js application ready to run and you can start coding to customize and improve your application. For more information, please visit the Vue CLI website:  https://cli.vuejs.org/ Want to see a live Vue.js application in production? Please visit and register the Dating web...

Angular 2+ with Internet Explorer IE9+

After published our latest Angular 7 application to production, we noticed that some of our customers couldn't access the app in Internet Explorer 11. Fortunately, there's a simple fix do make it work. All you need is to edit your polyfills.ts file located on the root of your project, and uncomment the lines after the Browser polyfills comment, as follows: And it's all! Hope to help someone!