Posts

What is promise, promise.all and promise.race? How do you chain promises?

A promise is an object that may produce a single value some time in the future : either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). A promise is an object which can be returned synchronously from an asynchronous function. It will be in one of 3 possible states: Fulfilled:   onFulfilled()  will be called (e.g.,  resolve()  was called) Rejected:   onRejected()  will be called (e.g.,  reject()  was called) Pending:  not yet fulfilled or rejected A promise is  settled  if it’s not pending (it has been resolved or rejected). Sometimes people use  resolved  and  settled  to mean the same thing:  not pending . Once settled, a promise can not be resettled. Calling  resolve()  or  reject()  again will have no effect. The immutability of a settled promise is an important feature. Promises following the spec must follow a specific set of rules: A promise or...

How do you create a hello world Node JS application

npm init npm install express --save var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); node app.js

What are clusters and worker threads, and when would you use them?

Worker threads are for when you want to run long-running synchronous tasks, and you don't want to block the event loop. It runs the code in a separate thread, and can communicate the results back to the main thread. Cluster workers are separate instances of your Node process, which are primarily used for load balancing incoming requests across multiple processes, to take advantage of multi-core processors.

What is V8 Engine? What is the relationship between Node.js and V8?

  V8 Engine is Google’s open source javascript and written in C++ and used inside Google Chrome. It was designed for increasing the performance of JavaScript execution inside web browsers. V8 is the Javascript engine inside of node.js that parses and runs your Javascript. The same V8 engine is used inside of Chrome to run javascript in the Chrome browser. Google open-sourced the V8 engine and the builders of node.js used it to run Javascript in node.js.

How to make Post request in Node.js?

  var request = require('request'); request.post(      'http://www.example.com/action',      { form: { key: 'value' } },      function (error, response, body) {           if (!error && response.statusCode == 200) {                console.log(body)           }      } );

What is the command used to import external libraries?

  The “ require ” command is used for importing external libraries.  Example: var http=require (“http”) It will load the http library . The single exported object through the http variable.

What is the difference between fork() and spawn() methods in Node.js?

  fork() spawn() fork() is a particular case of spawn() that generates a new instance of a V8 engine. Spawn() launches a new process with the available set of commands. Multiple workers run on a single node code base for multiple tasks. This method doesn’t generate a new V8 instance and only a single copy of the node module is active on the processor.