nodejslearnerfiles
Version:
Very Good Beginnings if you want to Learn Node.js
35 lines (22 loc) • 1.29 kB
Plain Text
One of the key concepts that "Node" brings from the browser to the JavaScript on the "server" is the "EventLoop".
In the browser, the "EventLoop" is constantly listening for DOM events such as key presses and mouse clicks.
Similarly, Node's "EventsLoop" is constantly listening for events on the Server Side.
These events can be externally generated, such as incoming HTTP requests or TCP connections or they can be timers and other
internal events, generated by your "Node" application itself.
Node has a non-blocking approach.
// Node Conventions for Callbacks.
getStuff(inputParam, handleResults);
// handleResults is the callback parameter.
// callback function is always a last parameter in the asynchronous functions call..
Here handleResults is a named callback function.
var handleResults = function(error, results) {
// if error is undefined...
// do something with results.
}
// Another convention around the use of call-backs is error handling. The first value passed to the callback is always, an error.
// Additionally it is a good practice to check if the callback function has succeeded by verifying the FALSE-E value at the top
of your callBack function.
// For simple, callbacks use Anonymous functions.
getStuff(inputParam, function(error, results) {
...
});