question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Retrying network request for set amount of time

See original GitHub issue

Hey, I understand this is a “how to” question, and this might not be the place for it. If so, feel free to close ticket, and I’ll open elsewhere.

I’m implementing payments against a very retro (I’m being nice here 😉 ) API, and can’t rely on the redirect after payment to contain the correct status. After payment, the payment provider makes a callback to a URL specified by us, which updates the order, which I have to check against. What I’d like to achieve in a semi-sensible way is this:

  • User lands on page, start saga
  • For 10 seconds (a race I imagine), call the order endpoint every 2 seconds to see if the order has the correct status
  • If the request “fails” (promise rejects), keep retrying
  • If the request “succeeds” (promise resolves), end race
  • After 10 seconds, if the request hasn’t succeeded, assume “fails”.

I understand the race concept and have implemented it elsewhere, I’m just not sure how to do the “retry every 2 seconds” and fit it in there. Any thoughts?

If you feel this warrants an extra page under “Advanced Concepts” I’d be happy to write it and contribute it back.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yelouaficommented, May 25, 2016

You’ll have to throw the error instead of returing it

function* checkOrderSaga() {
  for(let i = 1; i <= 5; i++) {
    try {
      const order = yield call(orderApi);
      return order;
    } catch(err) {
      if(i < 5) {
        yield call(delay, 2000);
      }
    }
  }
  // attempts failed after 5x2secs
  throw new Error('Order not fulfilled. Giving up.');
}

EDIT the code return Error was just for illustration

0reactions
yelouaficommented, May 25, 2016

yes; throwing instead of returning is because you catch in the containing saga, so you’re expecting the subsaga to throw errors instead of returning them

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrying a network request with a delay in Combine
This code will fire a network request, and if the request fails it will be retried three times. That means that at most...
Read more >
Retrying HTTP Requests a Fixed Number of Times With a ...
A guide on retrying sequential or parallel HTTP requests a fixed number of times with a delay using RXJS, Angular.
Read more >
How to retry network request for specified amount of time
On the first try of the request you start it. If it its slot gets called you cancel the current request and abort....
Read more >
Configuring request and retry timeout values - IBM
Set the request retry timeout value in a client property file. The requestRetryTimeout value is specified in milliseconds. In the example, if ...
Read more >
Control when a request will retry, and how long it will wait ...
Otherwise, httr2 will use "truncated exponential backoff with full jitter", i.e. it will wait a random amount of time between one second and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found