如何修复父forEach,直到完成子forEach

时间:2019-04-27 10:01:11

标签: angular foreach async-await angular-promise

我们正在使用 Angular v6.x

我们已经编写了用于读取zip文件的forEach代码,并称为了回调方法 回调方法中有一个Blob文件列表

我的代码

private _zipFileReader(inputfile, _processZipFilesList) {
  const filesList = [];
  zip.createReader(new zip.BlobReader(inputfile), (reader) => {
    reader.getEntries((entries) => {
      // setTimeout(() => {
        // Assume parent froEach..
        entries.forEach((entry, index) => {
          const filename = entry.filename;
          console.log('log-1 ' + index, entry, index);
          // Assume child forEach
          entry.getData(new zip.BlobWriter(), (blob) => {
            blob.name = filename;
            filesList.push(blob);
            console.log('log-2 ' + index, blob);
            if (index === (entries.length - 1)) {
              const jsonObj = {
                FilesList: filesList
              };
              console.log('log-3 finally ', filesList);
              _processZipFilesList(filesList);
            }
          });
        });
      // }, 500);
    });
  });
}

我们有uploadtest.zip个文件(共5个文件)的控制台日志打印内容

log-1 0 Entry 0
log-1 1 Entry 1
log-1 2 Entry 2
log-1 3 Entry 3
log-1 4 Entry 4
log-1 5 Entry 5
log-2 3 Blob
log-2 5 Blob
log-3 finally  Array
  • 仅从日志中以 entry.getData(new zip.BlobWriter(), (blob) => {
  • 处理的索引3和5

我尝试了等待和承诺,但是没有运气,我以为这发生在forEach

 index = 1 processing entry.getData() -->
 index = 2 still entry.getData() --> prcessing
 index = 3 still entry.getData() --> prcessing
 index = 4 processing entry.getData() --> 
 ..etc

如何解决这个问题?

0 个答案:

没有答案