3 Things you must know before attending NodeJS interview

Dani Vijay
WebClub.io
Published in
2 min readMar 18, 2020
Photo by rawpixel.com from Pexels

Node.JS is without a doubt a revolutionary invention. Not only to the JavaScript world, but for the whole web world itself. No matter you are a JS magician or just stepping into this scene, it is essential to know some basic concepts to successfully crack interviews on this open-source, cross-platform JavaScript run-time environment.

What is Node.JS?

By definition, Node.js is a JavaScript run-time built on Chrome’s V8 JavaScript engine. V8 engine parses your code and converts it into executable commands. Run-time is responsible for providing some objects which will help to interact with the outside world, such as file system.

How it works?

We are supposed to mention a bunch of keywords and explain how they work together to make the magic at this point. Unlike traditional server-side technologies which utilise multi-threaded architecture, the event-driven nature enables Node.JS to work as single threaded. An event loop is utilised to implement the model. But remember that only the main event loop is single threaded, but most of the I/O works run on separate threads, because the I/O APIs in Node.js are asynchronous/non-blocking by design.

Callbacks vs Promises (vs Asyc-Await)

Functions are non-blocking, which executes concurrently in Node.JS. Callbacks are utilised to signal completions of failures, hence efficiently manages dependent tasks. They are just functions passed as arguments which executes according to programmers will. But this can become messy over time, when there are a number of functions which depends each other. We, programmers call this ‘callback hell’.

doSomething(function(x){
doSomethingWithSomething(x, function(y){
doSomethingMore(y, function(z){
...
});
});
});

Promises provided an alternative solution to this problem. First, let’s create one.

function asyncOperation(optionalParams){
return new Promise(function(resolve, reject){
getData(optionalParams, function(error, result){
if(error){
reject(error);
}
else{
resolve(result);
}
})
});
}

Then we can make use of it:

asyncOperation(optionalParams)
.then(function(result){
// Do something with the result
})
.catch(function(error){
// Handle error
});

Finally, we got async await, which provides a more elegant solution to the same problem. It is a part of the ES7 standard, and implemented in version 7.6 of NodeJs. This is how we can utilise the same promise we’ve built previously.

async function getThatAsyncData(value){
const result = await asyncOperation(optionalParams);
return result;
}

Wait a minute! What if we run into an error? For error handling, simply wrap it in try-catch, and we’re good to go.

async function getThatAsyncData(value){
try {
const result = await asyncOperation(optionalParams);
return result;
}
catch(error){
// Handle error
}
}

Please keep in mind that this post is intended to provide an idea of where to start. You should dig deeper on the concepts to excel in your interviews.

All the best!!

--

--