Monday, November 16, 2015

Node.js async in practice

Error-first callback:
  • The first argument of the callback is reserved for an error object. If an error occurred, it will be returned by the first err argument.
  • The second argument of the callback is reserved for any successful response data. If no error occurred, err will be set to null and any successful data will be returned in the second argument.
Common node.js pattern suggests that the first argument of the callback function is an error.

Module

Node.js has a simple module loading system. In Node.js, files and modules are in one-to-one correspondence.

Variables local to the module will be private.

A required module prefixed with '/' is an absolute path to the file. For example, require('/home/marco/foo.js') will load the file at /home/marco/foo.js.

A required module prefixed with './' is relative to the file calling require(). That is, circle.js must be in the same directory as foo.js for require('./circle') to find it.

Without a leading '/', './', or '../' to indicate a file, the module must either be a core module or is loaded from a node_modules folder.

module.exports is the object that's actually returned as the result of a require call.

JS Library

Underscore.js is a utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects.



Ref:
http://www.sebastianseilund.com/nodejs-async-in-practice
http://amirrajan.net/nodejs-by-example/