Q1.
What is an error-first call back ?
Error-first callbacks are
used to pass error and data. The first argument to these functions is an error
object and the subsequent arguments represent associated data. So, you can
check the first argument i.e, the error object to see if something went wrong
and may be handle it. In case there is no error you can use the subsequent
arguments and go ahead.
e.g.
var post = new
Post({title: 'test'});
post.save(function(err,savedInstance){
if(err){
//handle error and return
}
//go ahead with `savedInstance`
});
The above snippet gives an example of
error-first callback.
Q2.
How can you avoid callback hells
As many of you probably know, all JavaScript
really is underneath is a single-threaded event loop that processes queued
events. If you were to execute a long-running task within a single thread then
the process would block, causing other events to have to wait to be processed
(i.e. UI hangs, data doesn't get saved, etc). This is exactly what you want to
avoid in an event-driven system. Here is
a great video explaining much more about the JavaScript event loop.
To solve this blocking problem,
JavaScript heavily relies on callbacks, which are functions that run after a
long-running process (IO, timer, etc) has finished, thus allowing the code
execution to proceed past the long-running task.
downloadFile('example.com/weather.json', function(err, data) {
console.log('Got weather data:', data);
});
Q3.
What are 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 may be in one of 3 possible states: fulfilled,
rejected, or pending. Promise users can attach callbacks to handle the
fulfilled value or the reason for rejection.
Q4.
Whats your favorite HTTP framework and why?
As
far as Node.js alternatives, Express is a standout amongst the most develop,
well-archived choices. It's steady, underpins a lot of modules and has an
incredible directing API. The system has a moderate layout that enables the
engineer to assume responsibility for each task and how it will be sorted out.
This gives incredible adaptability however can be somewhat overpowering to the
individuals who are new to Node.js.
Q5. What's a stub? Name a utilization case.
Stubs
are capacities/programs that reproduce the practices of parts/modules. Stubs
give canned responses to capacity calls made during experiments. Additionally,
you can state on with what these stubs were called.
A
utilization case can be a document perused when you would prefer not to peruse
a real record:
var
fs = require('fs');
var
readFileStub = sinon.stub(fs, 'readFile', work (way, cb) {
return
cb(null, 'filecontent');
});
expect(readFileStub).to.be.called;
readFileStub.restore();
Q6. Why use Node.js?
·
Node.js makes building scalable network programs easy. Some of its
advantages include:
·
It is generally fast
·
It almost never blocks
·
It offers a unified programming language and data type
·
Everything is asynchronous
·
It yields great concurrency
Q7. How else can the JavaScript code below be written using
Node.Js to produce the same output?
console.log("first");
setTimeout(function() {
console.log("second");
}, 0);
console.log("third");
setTimeout(function() {
console.log("second");
}, 0);
console.log("third");
Output:
first
third
second
third
second
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");
setImmediate(function(){
console.log("second");
});
console.log("third");
Q8.How do you prevent/fix callback hell?
The three ways to prevent/fix callback hell are:
·
Handle every single error
·
Keep your code shallow
·
Modularize – split the callbacks into
smaller, independent functions that can be called with some parameters then
joining them to achieve desired results.
The first level of improving the code above might
be:
var logError = function(error){
if(!error){
console.log("success!!");
}else{
console.log("error");
}
},
updateTransaction = function(t){
query("UPDATE transactions SET value = " + (t.value*0.1) + " WHERE id=" + t.id, logError);
},
handleTransactions = function(transactions){
transactions.each(updateTransaction);
},
handleClient = function(id){
query("SELECT * FROM transactions WHERE clientId=" + id, handleTransactions);
};
if(!error){
console.log("success!!");
}else{
console.log("error");
}
},
updateTransaction = function(t){
query("UPDATE transactions SET value = " + (t.value*0.1) + " WHERE id=" + t.id, logError);
},
handleTransactions = function(transactions){
transactions.each(updateTransaction);
},
handleClient = function(id){
query("SELECT * FROM transactions WHERE clientId=" + id, handleTransactions);
};
query("SELECT clientId FROM clients WHERE
clientName='picanteverde';",handleClient);
You can also use Promises, Generators and Async
functions to fix callback hell.
Q9. What is the difference between
AngularJS and Node.js?
Angular.JS is a web application development
framework while Node.js is a runtime system.
Q10. Why is consistent style important
and what tools can be used to assure it?
Consistent style helps team members modify projects
easily without having to get used to a new style every time. Tools that can
help include Standard and ESLint.
0 Comments