Make any function as awaited function in Node.js

Vrutik Prajapati
Code Dementia
Published in
3 min readSep 30, 2021

Suppose you are working with a function (Which is by default None-blocking in nature in Node.js) and in some sense, you want that until the evaluation of that function doesn’t complete, function execution has to wait there. Because in your further execution of code you need the result of that function.

Now the question comes: what is the need for an awaited function? Consider the following situation.

  • Suppose You are injecting dynamic data into the website. In that, you need to fetch data from the database or any other API. Fetching of data takes some time. So function execution has to wait until the data is not fetched.

Does it look theoretical? Let me elaborate on this.

For example, consider you are dealing with reading and writing files using the node’s default fs module. Consider the following code snippet:

Example of file reading
File reading example in Node.js

What will this code do? It will read the file located at the path. Check more about this function here.

What do you think? What will be the output of this code? If you thought that it would be the value of data then you were wrong! It will print null to console due to the Non-blocking nature of the Node function.

This is not acceptable, isn’t it? Because first temp() will be called then after, temp() will call getData(). Now getData() will call readfile() and it will assign the value of data to val then after we are printing val to the console. Now, This is happening due to the Non-blocking nature of readfile() means function execution will not wait until the reading of the file is not completed. It will move to the next block and the reading file will be running parallel to it. Hence, null will be printed(as the reading of the file will take some time).

Now to get rid of it, make getData() function as the awaited function. How to do that? Here are ways to do this:

  1. Using ‘Promises
  2. Using ‘util’ library in Node.js

Using ‘Promises’

In this method, you have to return a promise from the function. Consider the following code snippet:

Example of async function using promise
Example of asynchronous function using promise

We have returned Promise from getData() function. Now we need to wait until reading of the file is not completed. Hence, we have used await keyword inside async temp() function which will force temp() to wait until Promise is not evaluated and then it will assign data to val.

Note: Be careful while using ‘async’ keyword it will force a ‘function’ to wait not the ‘code execution’ means if you have written some code after temp() function then it will not force further block of code to wait. If you want it then use synchronous version of it. check it out here.

Now you will get what you want and your webpage will not show any unexpected output 🙂

Let’s see another method.

Using ‘util’ library in Node.js

Consider the following code snippet:

Example of async function using util library
Example of asynchronous function using util library

promisify() function takes a function as a parameter and will return a promised version of that function. Check out more about promisify() here.

The second approach looks easy, isn’t it? (As humans always try to find shortcuts 🙂). But the basic concept should be in your mind.

Now go through your web application, fetch some data without worrying about unexpected output.

Here is some reference material if you want to deep dive into it:

  1. https://nodejs.org/api/util.html#util_util_promisify_original
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Happy development!

--

--