最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Retrying a failed asyncpromise function? - Stack Overflow

programmeradmin3浏览0评论

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 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?

Share Improve this question asked Dec 30, 2018 at 22:45 mbilyanovmbilyanov 2,5117 gold badges31 silver badges53 bronze badges 4
  • Put it in a function. Call it. – Bergi Commented Dec 30, 2018 at 22:46
  • 2 Notice that you should put a limit on how often to retry, or even a backoff delay, so that you don't get into an infinite loop and possibly stress out whatever resource in test or doSomething is failing. – Bergi Commented Dec 30, 2018 at 22:48
  • Like, using a setTimeout()? – mbilyanov Commented Dec 30, 2018 at 22:49
  • Yes, something like that – Bergi Commented Dec 30, 2018 at 22:59
Add a comment  | 

4 Answers 4

Reset to default 16

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.

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)

You can put it in a function.

function dbug() {

test().then(function(result){
    // Success: Do something.
    doSomething();
}).catch(function(error){
    // Error: Handle the error, retry!
    dbug()
});
}

You could put the whole thing into a function that recursively calls itself if the catch block is entered:

function tryTest() {
  return test().then(function(result) {
    // Success: Do something.
    doSomething();
  }).catch(function(error) {
    // error handling

    // make sure to return here,
    // so that the initial call of tryTest can know when the whole operation was successful
    return tryTest();
  });
}


tryTest()
  .then(() => {
    console.log('Finished successfully');
  });

If your doSomething can take the result argument, and if tryTest doesn't take any arguments, you can simplify the above to:

function tryTest() {
  return test()
    .then(doSomething)
    .catch(tryTest);
}


tryTest()
  .then(() => {
    console.log('Finished successfully');
  });
发布评论

评论列表(0)

  1. 暂无评论