17
目前,我正在学习如何在承诺与FS和蓝鸟
的NodeJS使用的承诺,所以我的第一个挑战是列出目录中的文件,然后让每个使用异步函数两个步骤的内容。我想出了以下解决方案,但有一个强烈的感觉,这是不是最优雅的方式来做到这一点,尤其是第一部分,我在哪里“转向”异步方法分为承诺
// purpose is to get the contents of all files in a directory
// using the asynchronous methods fs.readdir() and fs.readFile()
// and chaining them via Promises using the bluebird promise library [1]
// [1] https://github.com/petkaantonov/bluebird
var Promise = require("bluebird");
var fs = require("fs");
var directory = "templates"
// turn fs.readdir() into a Promise
var getFiles = function(name) {
var promise = Promise.pending();
fs.readdir(directory, function(err, list) {
promise.fulfill(list)
})
return promise.promise;
}
// turn fs.readFile() into a Promise
var getContents = function(filename) {
var promise = Promise.pending();
fs.readFile(directory + "/" + filename, "utf8", function(err, content) {
promise.fulfill(content)
})
return promise.promise
}
现在链中承诺:
getFiles() // returns Promise for directory listing
.then(function(list) {
console.log("We got " + list)
console.log("Now reading those files\n")
// took me a while until i figured this out:
var listOfPromises = list.map(getContents)
return Promise.all(listOfPromises)
})
.then(function(content) {
console.log("so this is what we got: ", content)
})
正如我上面写的,它返回所需的结果,但我敢肯定有一个更优雅的方式来做到这一点。
不错,同时指出以后的读者可以使用'Async'函数:'readdirAsync','readFileAsync'。 – Wtower