Node JS

Node JS

by lshettyl

HTML

> What javascript engine Node uses?
> How do you update NPM to a new version in Node.js?
> Why is Node.js Single-threaded?
> Explain callback in Node.js.
> What is callback hell in Node.js? (nested callbacks) How do you prevent/fix? (Modularize, Promises, Generators)
> Name the types of API functions in Node.js. (blocking(readFileSync)/non-blocking(readFile))
> first argument typically passed to a Node.js callback handler
> What is NPM? how do you update NPM? How is package manager tied to it? tilde and caret, deps and dev deps
> What are streams (read and write data in continuous fashion), type of streams (read, write, read & write and duplex streams) and methods supported by streams
> What are exit codes in Node.js (used to end a “process”) ? List some exit codes
> Why is consistent style important and what tools can be used to assure it?
> What is the difference between AngularJS and Node.js?
> check the already globally installed dependencies? (npm ls -g)
> What is Event Loop?
> What is Piping in Node?
> Get file info ( fs.stat(path, callback) )
> __filename and __dirname
> What is REPL in context of Node?

What's wrong with the code snippet?
new Promise((resolve, reject) => {
  throw new Error('error')
}).then(console.log)
> As there is no catch after the then. This way the error will be a silent one, there will be no indication of an error thrown.

What's the output of following code snippet?
Promise.resolve(1)
  .then((x) => x + 1)
  .then((x) => { throw new Error('My Error') })
  .catch(() => 1)
  .then((x) => x + 1)
  .then((x) => console.log(x))
  .catch(console.error)
> The short answer is 2

JS:
console.log("first");
setTimeout(function() {
    console.log("second");
}, 0);
console.log("third");

In Node.js version 0.10 or higher, setImmediate(fn) will be used in place of setTimeout(fn,0) since it is faster. As such, the code can be written as follows:
console.log("first");
setImmediate(function(){
    console.log("second");
});
console.log("third");