- Callback
In the callback model you make a request and provide a function to be called when the request is completed. Which means one request and one reply.
Also in the callback approach you won’t receive any result until you receive all the results. Like shown in the example below, until it iterates over all the items it won’t return the result. The callback will not be invoked until the entire items list is iterated.
It will either display result or error.
getallItems(param, function(err, items){ //check for error //do something with items(an array of items) })
- Events
In this approach we can invoke the on function repeatedly until we iterate overall items.
It’s based on the Publish/Subscribe model, means multiple on events can be invoke repeatedly meaning subscribing to the events.
In the code below functions associated with the each item will be invoke each time until it reaches last item, so it gives the opportunity to act on the first item as soon as it arrives and publish the partial result.
var result = getallItems(param); result.on('item', function(i){ //do something with this item }); result.on('done', function(i){ //no more items left }); result.on('error', function(err){ //handle error })