В следующем коде.Я запускаю цикл для свойств formFields
.Как видите, я использую счетчик для запуска this.updatePanoCollection()
, только когда все файлы были загружены с api.uploadFieldFile
:
// formFields = { logo: { file: ... }, thumbnail: { file: ... } }
let toUploadCount = 0
let uploadedCount = 0
Object.entries(formFields).forEach(([key, upload]) => {
if (!upload || !upload.file) return
toUploadCount++
api.uploadFieldFile('logo', upload.file).then(downloadUrl => {
formFields[key] = downloadUrl
const updatePayload = {
id: this.currentPanoCollection.id,
data: formFields
}
uploadedCount++
if (toUploadCount === uploadedCount) {
// This only runs once right now
return this.updatePanoCollection(updatePayload)
}
}).then(() => {
// But this runs twice. It should only run once.
}).catch(err => this.handleError(err))
})
Теперь проблема в том, что код внутри .then()
запускаетсядважды.
Как изменить этот код, чтобы он запускался только один раз (после загрузки всех файлов)?