仅在返回诺言的方法完成后才调用 [英] call then only after method returning promise is finished

查看:86
本文介绍了仅在返回诺言的方法完成后才调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

submitTCtoDB() {
  console.log("this.selectedFileList is:  " + this.selectedFileList)
  this.readFile().then(() => {
    alert("ReadFile Finished now submit TC");
    this.submitTC()
  });
}

readFile() {
  return new Promise((resolve, reject) => {
    for (let i = 0; i < this.selectedFileList.length; i++) {
      let file = this.selectedFileList[i];
      alert("file in redafile" + file.name)
      let fileReader = new FileReader();
      fileReader.onload = () => {
        this.fileContent = fileReader.result;
        if (this.fileContent.indexOf("END DATA | BEGIN RESULTS") != -1) {
          alert("Multiple testcases found in " + file.name + " file.  Please separate/save testcases in Calc Builder. Then reimport");
          const index: number = this.selectedFileList.indexOf(file);

          if (index > -1) {
            this.selectedFileList.splice(index, 1);
          }

          console.log(this.fileContent);

        }
        resolve(this.fileContent);
      }
      fileReader.readAsText(file);
    }
  });
}

我只想在readFile方法完全完成之后才运行commitTC()方法,但是.then(内部submitTCtoDB)将被提早调用.

I want to run the submitTC() method only after the readFile method is completely finished but .then(inside submitTCtoDB) is getting invoked early .

我认为.then或promise没有正确使用.

I think .then or promise is not used properly.

所需的功能是仅在readFile方法完成读取/拆分文件后才调用SubmitTC方法. 请帮助.

Desired functionality is to call the submitTC method only when readFile method is completed reading/splicing the files. Kindly help.

推荐答案

您在循环中有一个resolve调用,但是resolve仅在第一次调用时才有效:一旦诺言解决,那就是它的实现最终状态,并触发then回调.因此,在读取 first 文件时发生这种情况,而无需等待其他文件被处理.

You have a resolve call in a loop, but resolve only has an effect when called the first time: once a promise resolves, that is its final state, and the then callbacks are triggered. So that happens when the first file has been read, without waiting for any other files to be processed.

您可以做什么:

  • 在不添加特定逻辑的情况下对FileReader进行承诺(您的if检查):将其保留在外部,因此它保持通用
  • 使用Promise.all将文件列表映射到将提供文件内容列表的新Promise.
  • 处理特定检查的内容列表
  • 将新的承诺(Promise.all或链接在其上的一个)返还给呼叫者.
  • Promisify the FileReader without adding specific logic (your if check): keep that outside of it, so it remains generic
  • Use Promise.all to map the file list to a new promise that will give the list of file contents.
  • Process the list of contents for the specific checks
  • Return the new promise (Promise.all or the one chained on it) to the caller.

代码:

submitTCtoDB() {
    console.log("this.selectedFileList is:  " + JSON.stringify(this.selectedFileList))
    this.readFileList(this.selectedFileList).then((validList) => {
        alert("ReadFile Finished now submit TC");
        this.selectedFileList = validList;
        this.submitTC()
    });
}

readFileList(list) {
    return Promise.all(list.map(file => this.readFile(file))).then(contents => {
        return list.filter((file, i) => {
            const fileContent = contents[i];
            if (fileContent.indexOf("END DATA | BEGIN RESULTS") != -1) {
                console.log("Multiple testcases found in " + file.name + " file.  Please separate/save testcases in Calc Builder. Then reimport");
                console.log(fileContent);
                return false; // exclude this file
            }
            return true; // include this file
        });
    });
}

readFile(file) {
    return new Promise(resolve => {
        console.log("file in promiseFile: " + file.name);
        const fileReader = new FileReader();
        fileReader.onload = () => resolve(fileReader.result);
        fileReader.readAsText(file);
    });
}

这篇关于仅在返回诺言的方法完成后才调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆