Said Pc
Said Pc

Reputation: 655

How to retry a rejected promise inside Promise.all in real time

I'm working on a file upload web app. I want to upload multiple files at the same time (5 for example) but when one of them fails I immediately show a RETRY button beside my file. When clicked I want it to retry its own Promise.

Regarding that I use Promise.all and all promises are inside Promise.all table how can I achieve that? I was thinking on using web workers but it seems too much for such a task.

Here's a snippet of code that I'm using (two files case):

await Promise.all([promise1.done(), promise2.done()]);

Upvotes: 0

Views: 760

Answers (2)

Said Pc
Said Pc

Reputation: 655

i found a solution as @RoboRobok and @Bergi said the solution was first (we can run parallel promises if we run them using promise.then ) and as @RoboRobok said we cannot reRun a failed promise instead we need to reproduce it again so what i did is creating a separated file called RetryUpload.js it has the code necessary to reproduce my Promise and that's all her's how i handled the promise (just for reference):

parallelUploads3
    .done()
    .then((success) => {
      .....
    })
    .catch((error) => {
     ....
    });

Upvotes: 0

Robo Robok
Robo Robok

Reputation: 22673

There's no such thing as retrying a Promise, because Promises do not keep their context. They are meant to change their status once and then they're unchangeable.

If you want to retry on failure, you'd need to explicitly call the action that resulted in your Promise again.

Upvotes: 1

Related Questions