我尝试使用 JavaScript 的 Promise 从路径列表中获取第一个路径(即 ["c:\\temp\\test1.json", "c:\\temp\\test2.json"])光盘上可用的文件(即 c:\temp\test2.json)。
function getFirstFile(paths) {
if (!paths || paths.length == 0) {
return {};
}
// sequential async search (recursive)
var filePath = paths.shift();
return fs.readFileAsync(filePath)
// found = stop searching
.then(function (content) {
return new Promise (function (resolve, reject) {
resolve(JSON.parse(content));
})
})
// continue searching further paths left in the list
.error(function (err) {
return getFirstFile(paths);
});
}
var paths2 = ["c:\\temp\\test1.json", "c:\\temp\\test2.json"];
getFirstFile(paths2)
.then( function (index) {
assert.equal(index.file, ".test2.json");
})
.error( function(err) {
assert.fail(err.toString());
});
由于某种原因,尽管文件“C:\temp\test2.json”可用,但 fs.readFileAsync(filePath) 未进入 .then(function (content) {
...就像 Promise 有异常或者没有捕获到什么?
最佳答案
为了清楚起见 -> 下面的代码无需 Mocha 测试即可正常工作。当集成到 Mocha 中时,无论我有 test2.json 做什么,测试都会顺利通过。
var fs = require('fs');
var Promise = require('bluebird'); //much faster than Chrome native promises
if (typeof fs.readFileAsync === "undefined") {Promise.promisifyAll(fs);} // generate Async methods from callbacks
var path = require('path');
function getFirstFile(paths) {
if (!paths || paths.length == 0) {
return {};
}
// sequential async search (recursive)
var filePath = paths.shift();
return fs.readFileAsync(filePath)
// found = stop searching
.then(function (content) {
return new Promise (function (resolve, reject) {
resolve(JSON.parse(content));
})
})
// continue searching further paths left in the list
.error(function (err) {
return getFirstFile(paths);
})
.catch(function (e) {
return getFirstFile(paths);
});
}
var paths2 = ["c:\\temp\\test1.json", "c:\\temp\\test2.json"];
getFirstFile(paths2)
.then( function (index) {
console.log(JSON.stringify(index));
})
.error( function(err) {
console.log(err);
});
错误在于 Mocha 测试的调用方式(请参阅错误和良好的测试代码)。
错误的代码(Mocha 总是会通过,因为 promise 在测试后返回):
it('My Test', function () {
utils.getFirstFile(["c:\\temp\\test1.json", "c:\\temp\\test2.json"])
.then(function(index) {
assert.equal(index.file, ".test2.json");
})
.catch( ....);
}
正确的代码是(返回 promise ):
it('My Test', function () {
return utils.getFirstFile(["c:\\temp\\test1.json", "c:\\temp\\test2.json"])
.then(function(index) {
assert.equal(index.file, ".test2.json");
});
}
关于javascript bluebird Promise -> 从异步列表中获取第一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32681305/