我很难弄清楚为什么'describeDir‘承诺链不能那么做..有人知道我搞砸了什么吗?所有代码似乎都会执行,但任何promise api函数,如then或executed,都永远不会执行。其中两个顶级函数如下所示。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);
});发布于 2017-07-29 09:59:46
首先,要检查是否存在拒绝,请尝试
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));
}作为奖励,下面是使用最新的ES2015+编码特性清理、扁平化和现代化的updateDescriptor函数(注释保持不变)。
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)
);https://stackoverflow.com/questions/45383700
复制相似问题