承诺解决问题

时间:2020-05-14 10:52:00

标签: javascript synchronization es6-promise

我正在尝试读取zip文件中存储的文件列表,并根据要求进行解析。

zip结构

文件夹1-> file1.json

文件夹2-> file2.json

到对象

{
  folder1: {file1: {file1json Data}},
  folder2: {file2: {file1json Data}}
}

如何等待

lis.reduce till 
return oFileReader.readFile(new File([current[1]], path[1]))
.then(oResult => { 
     accumulator[path[0]].data = oResult
})

返回结果并将其存储在累加器对象中

readZipFile: function(oFile) {
    return new Promise(function(resolve, reject) {
        return JSZip.loadAsync(oFile).then(function(zip) {
            var entries = Object.keys(zip.files).map(function(name) {
                return zip.files[name];
            });
            var listOfPromises = entries.map(function(entry) {
                return entry.async("blob").then(function(data) {
                    return [entry.name, data];
                });
            });
            var promiseOfList = Promise.all(listOfPromises);
            promiseOfList.then(function(list) {
                var result = list.reduce(function(accumulator, current) {
                    let currentName = current[0];
                    let path = currentName.split("/");

                    if (currentName.includes('.json')) {
                        let oFileReader = new new FileReader();
                        return oFileReader.readFile(new File([current[1]], path[1]))
                            .then(oResult => {
                                accumulator[path[0]].data = oResult
                            })
                    }
                    return accumulator;
                }, {});
                resolve(result);
            });
        });
    }.bind(this));
},

0 个答案:

没有答案