无法弄清楚为什么 'describeDir' promise 链当时不可用。任何人都知道我在这里搞砸了什么?所有的代码似乎都在执行,但是任何 promise 的 api 函数,例如 then 或 finally 都不会被执行。下面显示了两个顶级功能。 github 存储库位于 https://github.com/PhoenixContactUSA/pcworx-doc-gen
function updateDescriptor(fileloc, wsName, outdir){
console.log('Updating descriptor file for: ' + wsName);
return new Promise(function(resolve, reject){
return getDescriptor(outdir).then(
(value) => {
let descriptorFile = value;
var comments = getComments(fileloc);
var variables = getVariables(fileloc);
//wait until both are completed before continuing
return Promise.all([comments, variables]).then((values) => {
//var descriptor = new Object();
//console.log(JSON.stringify(descriptor));
descriptorFile[wsName] = new Object();
//console.log(JSON.stringify(descriptor));
//var worksheet = new Object();
descriptorFile[wsName].comments = values[0];
descriptorFile[wsName].variables = values[1];
//save the file
return saveDescriptor(descriptorFile, outdir).then((value) => {
console.log('Completed ' + wsName + ' ' + value);
resolve(value);
}, (reason) => {console.log(reason)})
}, (reason) => {
console.log(reason);
}
)
},
(reason) => {console.log(reason)}
)
})
}
function describeDir(filedir, outdir){
var files = findFilesInDir(filedir, '.XML');
for (var k=0;k<files.length;k++){
if ((files[k].indexOf('@HW') !== -1) || (files[k].indexOf('@LIBS') !== -1) || (files[k].indexOf('@ROOT') !== -1) || (files[k].indexOf('run') !== -1)) {
files.splice(k,1);
}
}
return Promise.each(files, function(file){
return updateDescriptor(file, path.basename(file), outdir);
});
}
然后我在这里调用函数。代码似乎执行得很好,但是 then() 从未被调用过。请注意,我在此最新版本中使用的是 bluebird。
//generate the output files, then copy them to the destination
docProcessor.describeDir(folder, path.join(__dirname, '..')).then((value)=>{
console.log('docProcessor then entered: ' + value);
});
最佳答案
首先,检查是否有拒绝,尝试
docProcessor.describeDir(folder, path.join(__dirname, '..'))
.then(value => console.log('docProcessor then entered:', value))
.catch(reason => console.error('error', reason);
describeDir
中的一个潜在问题是您使用 @HW
@LIBS
@ROOT< 过滤掉文件的循环
或名称中的 run
当你在 k
上拼接文件数组时,k++
仍然会被执行,因此你将跳过测试下一个文件
即
array = [a, b, c, d];
k == 1 // testing "b"
array.splice(k, 1);
now array = [a, c, d]
k++; // == 2
next iteration checks "d"
因此,如果连续有两个文件包含其中一个字符串,您将跳过“删除”它 - 这可能是问题所在?
你想改用过滤器
function describeDir(filedir, outdir) {
var files = findFilesInDir(filedir, '.XML')
.filter(file =>
file.indexOf('@HW') == -1 &&
file.indexOf('@LIBS') == -1 &&
file.indexOf('@ROOT') == -1 &&
file.indexOf('run') == -1
);
return Promise.each(files, file => updateDescriptor(file, path.basename(file), outdir));
}
或更整洁
function describeDir(filedir, outdir) {
var files = findFilesInDir(filedir, '.XML')
.filter(file => !/@HW|@LIBS|@ROOT|run/.test(file));
return Promise.each(files, file => updateDescriptor(file, path.basename(file), outdir));
}
作为奖励,以下是 updateDescriptor
函数,经过清理、扁平化和现代化,具有最新的 ES2015+ 编码功能(您的评论完好无损)
function updateDescriptor(fileloc, wsName, outdir) {
console.log('Updating descriptor file for: ' + wsName);
return getDescriptor(outdir)
.then(value => Promise.all([getComments(fileloc), getVariables(fileloc), value]))
.then(([comments, variables, descriptorFile]) => {
//var descriptor = new Object();
//console.log(JSON.stringify(descriptor));
//console.log(JSON.stringify(descriptor));
//descriptorFile[wsName] = new Object();
//var worksheet = new Object();
descriptorFile[wsName] = {comments, variables};
//save the file
return saveDescriptor(descriptorFile, outdir)
}).then((value) => {
console.log('Completed ' + wsName + ' ' + value);
return value
})
}
请注意缺少 catch
代码,因为您希望错误在链中持续存在
updateDescriptor
的真正紧凑版本是
const updateDescriptor = (fileloc, wsName, outdir) => getDescriptor(outdir)
.then(value => Promise.all([getComments(fileloc), getVariables(fileloc), value]))
.then(([comments, variables, descriptorFile]) =>
saveDescriptor(Object.assign(descriptorFile, {
[wsName] : { comments, variables }
}), outdir)
);
关于javascript - Promise 链当时不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45383700/