Node.js承诺Q.all不起作用

我有这个readLines函数来逐行parsing,调用从:

var fs = require('fs'); var Q = require('q'); Q.all(readLines(fs.createReadStream("/tmp/test.txt"), console.log)).then(function () { console.log('Done'); }; function readLines(input, func) { var remaining = ''; input.on('data', function (data) { remaining += data; var index = remaining.indexOf('\n'); while (index > -1) { var line = remaining.substring(0, index); remaining = remaining.substring(index + 1); func(line); index = remaining.indexOf('\n'); } }); input.on('end', function () { if (remaining.length > 0) { func(remaining); } }); }; 

任何人都可以帮助我为什么从来没有“完成”? 任何教程来了解Promise如何工作?

这对你会更好。 请阅读代码中的注释 – 实际上只有几行,包括添加error handling。

 var fs = require('fs'); var Q = require('q'); // you don't need Q.all unless you are looking for it to resolve multiple promises. // Since readlines returns a promise (a thenable), you can just then() it. readLines(fs.createReadStream("./vow.js"), console.log).then(function (x) { console.log('Done', x); }); function readLines(input, func) { // you need to create your own deferred in this case - use Q.defer() var deferred = Q.defer(); var remaining = ''; input.on('data', function(data) { remaining += data; var index = remaining.indexOf('\n'); while (index > -1) { var line = remaining.substring(0, index); remaining = remaining.substring(index + 1); func(line); index = remaining.indexOf('\n'); } }); input.on('end', function() { if (remaining.length > 0) { func(remaining); console.log('done'); } // since you're done, you would resolve the promise, passing back the // thing that would be passed to the next part of the chain, eg .then() deferred.resolve("Wouldn't you want to return something to 'then' with?"); }); input.on('error', function() { console.log('bother'); // if you have an error, you reject - passing an error that could be // be caught by .catch() or .fail() deferred.reject(new Error('Regrettable news - an error occured.')); }); // you return the promise from the deferred - not the deferred itself return deferred.promise; };