I have written following code for uploading files on the server asynchronously with the help of promises. And we know that promise.all will fail once anyone of the promises will fail. So, i want to to know which promise is actually failed and in my case name of file for which the promise was failed. I am trying console.log(e1) but it is not giving me information about failed promise. Can anyone please help me do it?
uploadFilesAndSendStatus(stateType, notes, estimate, visibleToCustomer = null)
{
let filesPromise = Promise.resolve([]);
const promises = this.state.files_to_upload.map((file) => {
return this.uploadFilesOnServer(file);
});
filesPromise = Promise.all(promises).then((results) => {
return [].concat(...results);
}).catch((e1) =>{
console.log(e1);
this.setState({
serverActionPending: false,
serverActionComplete: false,
file_upload_try_again: true,
});
});
}
UploadFilesOnServer code is:
uploadFilesOnServer(file) {
let files=[];
let file_id='';
const image=file;
const promise = getAttachmentUploadURL(this.props.task.id)
.then((imageUrlResponse) => {
const data = new FormData();
data.append('file-0', image);
const { upload_url } = JSON.parse(imageUrlResponse);
return uploadAttachment(upload_url, data);
})
.then ((updateImageResponse) => {
file_id= JSON.parse(updateImageResponse);
files.push(file_id);
return files;
});
return promise;
}