Events and Streams - xuanz1993/NodeJS GitHub Wiki

  • implement asynchronous non-blocking code:
  1. callbacks: make a request and provide a function to be called when the request is completed, one request one reply; no results until all results; either results or errors
getItem(parameter, function(err, results){
  //check for error
  // operate results
2. events, invoke _.on()_ function repeatedly to provide multiple functions to invoke on each event; act on results as they arrive; partial results before error

var results = getItem(parameter); results.on('item', function(i){ // do something with this one item }); results.on('done',function(){ // No more items }); results.on('error', function(){ // react to error });

* Node's "EventEmitter" class
1. A return value from a function call
2. object extends the EventEmitter class
* Streams in Node.js
1. streams are instances of (and extensions to)