首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Promise chain not then-able

Promise chain not then-able
EN

Stack Overflow用户
提问于 2017-07-29 07:25:45
回答 1查看 89关注 0票数 1

我很难弄清楚为什么'describeDir‘承诺链不能那么做..有人知道我搞砸了什么吗?所有代码似乎都会执行,但任何promise api函数,如then或executed,都永远不会执行。其中两个顶级函数如下所示。github存储库位于https://github.com/PhoenixContactUSA/pcworx-doc-gen

代码语言:javascript
复制
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。

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

回答 1

Stack Overflow用户

发布于 2017-07-29 09:59:46

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

代码语言:javascript
复制
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++,因此您将跳过测试下一个文件

代码语言:javascript
复制
array = [a, b, c, d];
k == 1 // testing "b"
array.splice(k, 1);
now array = [a, c, d]
k++; // == 2
next iteration checks "d"

因此,如果有两个文件中包含其中一个字符串,您将跳过“删除”它-这可能是问题所在?

你想改用过滤器

代码语言:javascript
复制
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));
}

或者更整洁

代码语言:javascript
复制
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函数(注释保持不变)。

代码语言:javascript
复制
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的一个真正的精简版本是

代码语言:javascript
复制
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)
    );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45383700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档