Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrying a failed async/promise function?

I have this async block:

test().then(function(result){
    // Success: Do something.
    doSomething();
}).catch(function(error){
    // Error: Handle the error, retry!
    // How to re-run this whole block?
});

I can keep track of the success and failed outcomes. However, is it possible to retry the whole test().then().catch() chain if we fail? And keep retrying until the condition resolves?

like image 747
mbilyanov Avatar asked Dec 30 '18 22:12

mbilyanov


2 Answers

If you can switch to async/await syntax, you can use a while loop:

let keepTrying;

do {
    try {
        await test();
        keepTrying = false;
    } catch {
        keepTrying = true;
    }
} while (keepTrying)

doSomething();

You could then abstract the retrying logic into its own function to be reused.

like image 127
Frank Modica Avatar answered Nov 16 '22 12:11

Frank Modica


Assuming it's all about resending request to some buggy/bloat-up 3rd party API

If it's production question rather educational one I'd suggest search for 3rd party lib that implementing this on your own.

Say for axios there is nice axios-retry.

Why? Assume you may think there is just one case when API say returns 502. But actually there are much more cases it'd be better to keep in mind:

  1. differing particular error causes, say once there is Network or DNS Lookup Error there may be no need to repeat request
  2. retry count limitation
  3. increasing delay
  4. something else

Writing such a logic on your own would be real overkill. And trying to use simplest solution may hit you when you don't expect it.

PS also as a bonus you would be able to configure all requests to some specific API with single snippet like it goes for axios' custom instances(and I believe there should other plugins for alternative libraries)

like image 3
skyboyer Avatar answered Nov 16 '22 12:11

skyboyer