我是node.js的新手,试图绕着承诺努力工作。我正在尝试从文件中读取内容,返回承诺和文件内容。我到目前为止能够阅读文件的内容并在控制台上打印它们,并返回一个承诺。我想要返回文件的内容。退货承诺和内容(可能很多)
这是我的代码到目前为止。
function() {
return fs.exists(listFile).then(function (exists) {
if(exists) {
return fs.readFile(listFile).then(function(response) {
console.log(response.toString());
}).catch(function(error) {
console.error('failed to read from the file', error);
});
}
}).catch(function(err) {
console.error('Error checking existence', err)
});
};
回答类似的问题:http://stackoverflow.com/a/39761387/4175944 – Christopher
您使用的蓝鸟? 'fs.exists()'不返回一个Promise。 – jessegavin
[如何正确获取从承诺返回的值?]可能重复(http://stackoverflow.com/questions/39761302/how-to-correctly-get-the-value-returned-from-a-promise) – Christopher