Database Reference
In-Depth Information
function doSecond(data) {
second(data,function(error,moreData) {
doThird(data,moreData);
});
}
first(a,function(error,data) {
doSecond(data);
});
But this style is actually fairly cumbersome to maintain in practice. Another
approach is to use an events library, part of the Node core libraries, to use
an EventEmitter to coordinate sequential events:
var coord = new require('events').EventEmitter();
coord.on('first',function(a) {
first(input,function(error,data) {
emit('second',data); }
});
coord.on('second',function(data) {
second(data,function(error,moreData) {
coord.emit('third',data,moreData);
}
});
coord.on('third',function(data,moreData) {
third(data,moreData,function(error,evenMoreData) {
});
});
This example is very similar to the previous example, but the similarity
between calls points to being able to create a library that can easily chain
calls together. In fact, this has already been done in a number of different
libraries that implement various techniques for arranging calls. One of these
libraries, which is used extensively in this topic, is called async and allows
the previous example to be implemented as a waterfall :
var async = require('async');
var input = ...;
async.waterfall([
function(cb) {
Search WWH ::




Custom Search