承诺链不可能

时间:2017-07-28 23:25:45

标签: javascript promise

无法弄清楚为何“描述”'承诺链是不可能的..任何人都知道我搞砸了什么?所有的代码似乎都在执行,但是任何promise api函数都是如此,或者最终永远不会被执行。两个顶级功能如下所示。 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()。请注意,我在最新版本中使用了蓝鸟。

  //generate the output files, then copy them to the destination
    docProcessor.describeDir(folder, path.join(__dirname, '..')).then((value)=>{
      console.log('docProcessor then entered: ' + value);
    });

1 个答案:

答案 0 :(得分:1)

首先,要检查是否有拒绝,请尝试

docProcessor.describeDir(folder, path.join(__dirname, '..'))
.then(value => console.log('docProcessor then entered:', value))
.catch(reason => console.error('error', reason);

describeDir中的一个潜在问题是您使用名称

@HW @LIBS @ROOTrun过滤掉文件的循环>

当您在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)
    );