I've developed a function that generates an array of promises:
async addDefect(payload) {
this.newDefect.setNote(payload.note);
this.newDefect.setPriority(payload.priority);
const name = await this.storage.get(StorageKeys.NAME);
const movingFilesJob = this.cachedPhotoUrls.map(url => {
const defectImage = this.newDefect.generateImageUrl(name);
return this.file.moveImageToAppFile(url, defectImage.url);
});
await Promise.all(movingFilesJob);
this.viewCtrl.dismiss(this.newDefect);
}
Now, I want to move the creation of movingFilesFob
to another class. So, I created the following function:
async persistPhotos(photoBuffer: string[], defect: Defect) {
const name = await this.storage.get(StorageKeys.NAME);
return photoBuffer.map(url => {
const defectImage = defect.generateImageUrl(name);
return this.file.moveImageToAppFile(url, defectImage.url);
});
}
When I try to replace the code, I'm encountering the error message:
Argument of type 'Promise[]>' is not assignable to parameter of type 'Iterable<{} | PromiseLike<{}>>'. Property '[Symbol.iterator]' is missing in type 'Promise[]>'
In my function call, it looks like this:
async addDefect(payload) {
this.newDefect.setNote(payload.note);
this.newDefect.setPriority(payload.priority);
const name = await this.storage.get(StorageKeys.NAME);
const movingFilesJob = this.photo.persistPhotos(this.cachedPhotoUrls, this.newDefect);
await Promise.all(movingFilesJob);
this.viewCtrl.dismiss(this.newDefect);
}
It's puzzling why the first example works fine while the second one doesn't. I tried assigning type :any to the return value but it didn't work during runtime.