У нас есть 2 функции:
submitEstateObject = () => {
// if (!this.state.formIsValid) {
// this.setState({ showFieldErrors: true })
// console.log(this.state)
// } else {
//this.setState({ isLoading: true })
console.log('Category', this.state.estateForm.category.value)
console.log('Type', this.state.estateForm.type.value)
console.log('Params', this.state.estateParams)
console.log('ContactsAndOwners', this.state.contactsAndOwners)
console.log('Photos', this.state.photos)
//Submit photos
const test = this.uploadPhotos() // WAIT UNTILL FUNC RETURN ARRAY!
//AFTER TEST WAS DONE RUN CODE BELOW
console.log('Test', test)
console.log('Test2')
//}
}
В теле моей функции вы можете видеть, что я звоню uploadPhotos
.Эта функция должна возвращать массив объектов, и после этого я хочу продолжить выполнение другого кода ниже.Как это сделать?
uploadPhotos = () => {
if (this.state.photos.length) {
const photosToSave = this.state.photos.map(photo => {
const extensionPattern = /(?:\.([^.]+))?$/
const fileName = photo.value.name
const fileExtension = extensionPattern.exec(fileName)[1]
const newFileName = new Date().getTime()
const fileLocation = storageRef.child('estateObjects/' + newFileName + '.' + fileExtension)
return new Promise(resolve => {
fileLocation.put(photo.value).on(
firebase.storage.TaskEvent.STATE_CHANGED,
snapshot => {
//let progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
},
null,
() => {
fileLocation.getDownloadURL().then(url => {
resolve({ url: url, file: newFileName + '.' + fileExtension })
})
}
)
})
})
Promise.all(photosToSave).then(response => {
return response
})
}
}