基本上我尝试从目录中读取所有图像文件,并获取每个文件的元数据以创建缩略图名称。然后将每个文件的名称,路径和缩略图名称放入一个数组中。抓取所有元数据后,我想运行另一个函数。 catch是函数需要完成文件操作和元数据的所有可观察对象。
我解决这个问题的方法是检查这是否是要处理元数据的数组中的最后一项,并假设每个observable将按照它们被调用的确切顺序完成。对我来说,这似乎不是100%的傻瓜证明。有更好的方法吗?
以下代码的片段:
this.fileAccess.listDir(this.OSRootPath, this.directoryPath).then(
(files)=>{
for(let i = 0; i < files.length; i++){
let fileName:string = files[i].name.toLowerCase();
// check if folder, if so copy its path and name
if (files[i].isDirectory){
this.directorySubDirectories.push({'path': files[i].nativeURL, "name": files[i].name});
} // check if file is a image type, by checking the extensions
else if (fileName.includes(this.jpgExtension) || fileName.includes(this.gifExtension) ||
fileName.includes(this.pngExtension) || fileName.includes(this.tifExtension)) {
files[i].getMetadata((metaData)=>{
let thumbnailName:string = this.generateThumbnailName(files[i].name,
metaData);
this.directoryImages.push({ 'name': files[i].name,
'path': files[i].nativeURL,
'thumbnailName': thumbnailName,
'thumbnailPath': ''});
// Here I'm checking to see if this is the last file in the array that needs to have it's meta data proccessed,
// if so I run a fuction that will check if the thumbnails already exists or it will create a new one
if (i == (files.length - 1)){
// run next function
this.processAllThumbnailsImages();
}
});
}
}
}
).catch(
(error) =>{
console.log("ERROR Reading the directory, code below:");
console.log(error);
}
);
});
}