3 Things you must know before attending NodeJS interview

Dani Vijay
Mar 18, 2020 · 2 min read
Image for post
Image for post
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 , 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!!

WebClub.io

Continues Learning for Web Developers

Dani Vijay

Written by

Full Stack Web Dev | Tech Evangelist | https://twitter.com/UtmostDev | https://github.com/danivijay/ | https://linkedin.com/in/danivijay

WebClub.io

Continues Learning for Web Developers

Dani Vijay

Written by

Full Stack Web Dev | Tech Evangelist | https://twitter.com/UtmostDev | https://github.com/danivijay/ | https://linkedin.com/in/danivijay

WebClub.io

Continues Learning for Web Developers

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store