繁体   English   中英

在函数链上返回承诺

[英]Returning promises up a function chain

下面是我写的一些代码的结构。 我正在尝试使用 ajax 将多个文件加载到服务器,一旦所有文件都完成,请执行一些操作。

function func1(items){
 const results = []
 for (let i=0; i<items.length; i++) {
    results[i] = func2();
 }

 Promise.all(results).then(response => some_action())
}

function func3(params) {

  return new Promise((resolve, reject) => {
    //ajax call here and resolve/reject
  })
}

function func2(){

 if(stuff){
   return func3(some_params);
 } else {
   return func3(other_params);   
 }

}

不幸的是,它没有按我预期的那样工作。 results数组不是promise数组,而是undefined数组。 我是 Javascript 承诺的新手,所以任何帮助将不胜感激。

编辑:为了回应关于无声返回可能性的评论,我发布了 func2 的实际代码(稍作修改):

function func2(item, id, id2, top_level, path){

  if(item.isFile){
    item.file(function(file) {
      if(file.name.substring(file.name.lastIndexOf('.')+1) === "docx"){
        file = new File([file], file.name, {
          type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        })
      }
      if(file.name !== "desktop.ini"){
        let url = 'url'
        return func3(file, id, url, "", false);
      }
    });
  } else {

      window.parent.ipcRenderer.send('zip-folder', path);

      window.parent.ipcRenderer.on('zipped', (event, buffer) => {
        var zipped_file = new File([buffer], item.name + ".zip", {
          type: "application/zip"
        })
        let url = "/url"
        return func3(zipped_file, id, url, id2, true);
      })

  }
}

您需要在每个函数中返回新的 Promise !

您的else块不返回任何内容:

...
else {

  window.parent.ipcRenderer.send('zip-folder', path);

  window.parent.ipcRenderer.on('zipped', (event, buffer) => {
    var zipped_file = new File([buffer], item.name + ".zip", {
      type: "application/zip"
    })
    let url = "/url"
    return func3(zipped_file, id, url, id2, true);
  })

  // silently returns undefined
}

不要被匿名函数中的 return 语句所迷惑。 它返回匿名函数,而不是func2

  window.parent.ipcRenderer.on('zipped',                         return this
                                         (event, buffer) => {  <----------.
      var zipped_file = new File([buffer], item.name + ".zip", {          |
        type: "application/zip"                                           |
      })                                                                  |
      let url = "/url"                                                    |
      return func3(zipped_file, id, url, id2, true);   -------------------'
    }
  )

如果您在不使用匿名函数的情况下重写代码,这将更加明显:

function func4(id, url, id2) {
  return function (event, buffer) {
    var zipped_file = new File([buffer], item.name + ".zip", {
      type: "application/zip"
    })
    let url = "/url"
    return func3(zipped_file, id, url, id2, true);
  }
}

function func2(item, id, id2, top_level, path){

  if(item.isFile){
    item.file(function(file) {
      if(file.name.substring(file.name.lastIndexOf('.')+1) === "docx"){
        file = new File([file], file.name, {
          type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        })
      }
      if(file.name !== "desktop.ini"){
        let url = 'url'
        return func3(file, id, url, "", false);
      }
    });
  } else {

      window.parent.ipcRenderer.send('zip-folder', path);

      window.parent.ipcRenderer.on('zipped', func4(id, url, id2))

      // no return statement !!
  }
}

绕过它的一种方法是将其转换为承诺:

return new Promise((ok,fail) => {
  window.parent.ipcRenderer.on('zipped', (event, buffer) => {
    var zipped_file = new File([buffer], item.name + ".zip", {
      type: "application/zip"
    })
    let url = "/url"
    ok(func3(zipped_file, id, url, id2, true));
  })
});

当然,根据您的错误逻辑如何流动,您可能希望在更高级别包装承诺。 这仅说明了快速修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM