Im đọc bài viết này: http://elegantcode.com/2011/04/06/taking-baby-steps-with-node-js-pumping-data-between-streams/ và gặp một số vấn đề nhỏ trong việc hiểu luồng.nodejs stream vs callbacks
Trích:
"Suppose we want to develop a simple web application that reads a particular file from disk and send it to the browser. The following code shows a very simple and naïve implementation in order to make this happen."
Vì vậy, các mẫu mã được như sau:
var readStream = fileSystem.createReadStream(filePath);
readStream.on('data', function(data) {
response.write(data);
});
readStream.on('end', function() {
response.end();
});
Tại sao chúng ta sẽ sử dụng cách trên khi chúng ta chỉ có thể làm:
fs.readFile(filePath, function(err, data){
response.write(data);
response.end();
});
Khi nào hoặc tại sao tôi nên sử dụng luồng?