Nodejs interview questions set 3


 

1)What is nonblocking io?

Ans: Non-blocking I/O operations allow a single process to serve multiple requests at the same time. Instead of the process being blocked and waiting for I/O operations to complete, the I/O operations are delegated to the system, so that the process can execute the next piece of code


2)Async await in foreach?

Ans:

Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)


For example, the following forEach loop might not do what it appears to do:


const players = await this.getWinners();


// BAD

await players.forEach(async (player) => {

  await givePrizeToPlayer(player);

});


await sendEmailToAdmin('All prizes awarded');

What's wrong with it?


The promises returned by the iterator function are not handled. So if one of them throws an error, the error won't be caught. (In Node 10, if no unhandledrejection listener has been registered, that will cause the process to crash!)

Because forEach does not wait for each promise to resolve, all the prizes are awarded in parallel, not serial (one by one).

So the loop actually finishes iterating before any of the prizes have finished been awarded (but after they have all started being awarded).

As a result, sendEmailToAdmin() sends the email before any of the prizes have finished being awarded. Maybe none of them will end up being awarded (they might all throw an error)!

3)What is callback hell?
Ans:
Callback hell in Node. js is the situation in which we have complex nested callbacks. In this, each callback takes arguments that have been obtained as a result of previous callbacks. This kind of callback structure leads to lesser code readability and maintainability.

4)What is the difference between local and global npm packages?
Ans: local packages are installed in the directory where you run npm install <package-name> , and they are put in the node_modules folder under this directory. global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run npm install -g <package-name>

5)What is Callback?
Ans: A callback is a function that you want to run sometime later, like when a specific event happens. For example, if you wanted to run a function foo() when a button is pressed, you pass foo as a callback like this: addEventListener("click", foo); That sets things up so that foo will automatically be called when your button is clicked.



6)Difference between callback() or return callback()  ?
Ans:
return callback() is just a clear way of short-circuiting the function you're in.
Also return will stop executing the function and if anything is written after the return it will not execute.

7) Can you use require module inside a function?
Ans: require() is on-demand loading. Once a module has been loaded it won't be reloaded if the require() call is run again. By putting it inside a function instead of your top level module code, you can delay its loading or potentially avoid it if you never actually invoke that function. However, require() is synchronous and loads the module from disk so best practice is to load any modules you need at application start before your application starts serving requests which then ensures that only asynchronous IO happens while your application is operational.

Also, check this scenario:
First scenario: imagine I had only modules a.js and b.js. Module a.js requires b.js and module b.js requires c.js. If they require at the top of the module I will get alerted when I launch my application that module c.js was not found and the server would crash before I ever went to deploy it. If b.js and c.js are required in functions the error may go undetected until the server is deployed, at which time the entire server will crash when Node attempts to load module c.js possibly several days later if the route requiring that module is one rarely used.


8)Explain how Nodejs handles child threads?
Ans:
Node.js, in its essence, is a single thread process. It does not expose child threads and thread management methods to the developer. Technically, Node.js does spawn child threads for certain tasks such as asynchronous I/O, but these run behind the scenes and do not execute any application JavaScript code, nor block the main event loop.

If threading support is desired in a Node.js application, there are tools available to enable it, such as the ChildProcess module.



Comments

Popular posts from this blog

Node JS:Understanding bin in package.json.

Node.js: create an excel file with multiple tabs.

Node.js: Downloading a xml file from given url and reading its data elements.