Database Reference
In-Depth Information
a function is bound to the line event that handles the processing of the
line of data. Other events, not shown in this example, may also be bound to
functions to allow the program to respond to the end of the file or an error
generated by the reading process:
var readline = require('readline');
var lines = readline.createInterface({
input: stream
});
lines.on('line',function(line) {
//Do something with line here
});
Escaping the “Callback Pyramid of Doom”
The callback style of programming is easy to follow when there is only one
set of events to process that does not require further calls. Of course, this
is never the case for nontrivial software, and the callback model begins to
break down. For example, sequential operations quickly become hard to
follow:
first(input,function(error,data) {
second(data,function(error,moreData) {
third(data,moreData,function(error,evenMoreData) {
//Do something with the data...
console.log("data: "+data+", moreData:"
+moreData+" evenMoreData:"+evenMoreData);
});
})
});
This is colloquially known as the “callback pyramid (of doom),” and it
troubles JavaScript programmers both on the front end and the back end.
There are stylistic approaches that can reorganize the pyramid by rewriting
the operations as separate named functions:
function doThird(data,moreData) {
third(data,moreData,function(error,evenMoreData) {
});
});
Search WWH ::




Custom Search