2

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;
  }
Hamid Raza
  • 185
  • 2
  • 3
  • 13

1 Answers1

3

You can add that information to the error object:

const promises = this.state.files_to_upload.map((file, i) => {
  return this.uploadFilesOnServer(file).catch(err => {
    const e = new Error("upload failed");
    e.index = i;
    e.filename = file
    throw e;
  });
});

const filesPromise = Promise.all(promises).then(res => [].concat(...res)).catch(e1 => {
  console.log(e1);
  …
});
Bergi
  • 572,313
  • 128
  • 898
  • 1,281
  • This will give me the first rejected promise. What if i want to get all rejected promise from promise.all? How can we alter promise.all in this case? – Hamid Raza Aug 29 '17 at 11:47
  • Then we can't use rejections but have to [Wait until all ES6 promises complete](https://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises) – Bergi Aug 29 '17 at 12:03
  • can you please tell me how can i code it in my example? – Hamid Raza Aug 29 '17 at 12:34
  • @HamidArrivy Quite similar to the code in my answer, just don't `throw` but `return` and then distinguish errors from successful results in the `then` callback – Bergi Aug 29 '17 at 12:37
  • In then callback of promise.all? – Hamid Raza Aug 29 '17 at 12:39
  • @HamidArrivy Yes, there. – Bergi Aug 29 '17 at 12:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153136/discussion-between-hamidarrivy-and-bergi). – Hamid Raza Aug 29 '17 at 12:50