如何与其他asynchronous模块一起使用asynchronous?

这是一个没有asynchronous的工作testing程序:

var fs = require('fs'); function test() { var finalResponse = '', response = ''; function showFinalResponse() { console.log(finalResponse); } function processTheFile(err, data) { if (err) { finalResponse = 'Could not read the file'; } else { response += data; response += '</body></html>'; finalResponse = response; } showFinalResponse(); } function readTheFile(exists) { if (!exists) { finalResponse = 'File does not exist.'; showFinalResponse(); } else { response += '<!DOCTYPE html><html lang="en-US"><head></head><body>'; fs.readFile('file.txt', 'utf8', processTheFile); } } fs.exists('file.txt', readTheFile); }; test(); 

这里是我试图获得同样的程序与asynchronous瀑布。 我在如何在asynchronous和fs调用中传递callback方面遇到问题。

 var fs = require('fs'); var async = require('async'); function testAsync() {var finalResponse, response = ''; async.waterfall( [ function checkIfTheFileExists(done) { fs.exists('file.txt', done); }, function readTheFile(err, exists, done) { response += '<!DOCTYPE html><html lang="en-US"><head></head><body>'; fs.readFile('file.txt', 'utf8', done); }, function processTheFile(err, data, done) { response += data; response += '</body></html>'; finalResponse = response; done(null); } ], function showFinalResponse(err) { if (err) { if (err.code === 'ENOENT') { // intended to test for file is missing. finalResponse = 'File does not exist.'; } else { // any other errors. finalResponse = 'Could not read the file'; } console.log(err); } console.log(finalResponse); } ); } testAsync() 

我不能让asynchronous版本工作。 我感到困惑的callback去。

fs.exists是一个古怪的东西,它不提供一个错误参数给它的callback函数。 相反,它只提供一个exists参数,指示文件是否被find。 据推测,如果有错误, exists将是false 。 因此,您需要将其callback包装在自己的函数中,以便您可以为waterfallcallback提供单独的错误参数:

 async.waterfall( [ function checkIfFileExists(done) { fs.exists('file.txt', function(exists) { done(null, exists); }); }, function makeSureFileExists(exists, done) { ... 

注意文档中的警告,但是, fs.exists不应使用fs.exists

 fs.exists('file.txt', done(null)); 

这个调用立即done 。 您需要将实际done函数传递给fs.exists

 fs.exists('file.txt', done); 

其他人也一样。

这是我最后的工作版本(以防别人帮忙)。 再次感谢你的帮助!

 var fs = require('fs'); var async = require('async'); var addErrParm = function (err, done) {return function(exists) { done(err, exists); }} function testAsync() {var finalResponse, response = ''; function checkIfTheFileExists(done) { fs.exists('file.txt', addErrParm(null, done)); } function readTheFile(exists, done) { if (!exists) { done('notFound'); } else { response += '<!DOCTYPE html><html lang="en-US"><head></head><body>'; fs.readFile('file.txt', 'utf8', done); } } function processTheFile(data, done) { response += (data || 'The file is empty') + '</body></html>'; finalResponse = response; done(null); } function showFinalResponse(err) { if (err) { finalResponse = (err === 'notFound' ? 'File does not exist.' : 'Could not read the file'); } console.log(finalResponse); } async.waterfall([ checkIfTheFileExists, readTheFile, processTheFile ], showFinalResponse); } testAsync() 

所以基本上,asynchronous需要从除最终callback以外的所有函数中删除err参数(第一个参数),并且除了最终callback以外,它需要在所有函数中添加callback('done')作为额外参数。

另外,如果没有像fs.exists那样的err参数,你必须创build一个函数来模拟一个err参数,这样async可以将其删除。