Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does the example code work? #56

Closed
robcalcroft opened this issue May 15, 2019 · 3 comments
Closed

Does the example code work? #56

robcalcroft opened this issue May 15, 2019 · 3 comments

Comments

@robcalcroft
Copy link

await retry(async bail => {
  // if anything throws, we retry
  const res = await fetch('https://google.com')
  // IF THIS CODE THROWS THEN THE CODE UNDERNEATH WON'T EVER RUN RIGHT?
 //  SO WHY IS THERE LOGIC TO BAIL IN THERE, BECAUSE SURELY TO RUN THAT CODE
// THE PROMISE WOULD HAVE RESOLVED CORRECTLY AND THEREFORE NOT NEED
// TO BE RETRIED?

  if (403 === res.status) {
    // don't retry upon 403
    bail(new Error('Unauthorized'))
    return
  }

  const data = await res.text()
  return data.substr(0, 500)
}, {
  retries: 5
})

my question is in caps above 😄

@robcalcroft
Copy link
Author

In this example those two console logs below the await are never run

const retry = require("async-retry");

function asyncThing() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log("running");
      reject("dogs");
    }, 500);
  });
}

(async () => {
  await retry(
    async () => {
      const result = await asyncThing();
      console.log(result);
      console.log("going and doing way more!");
    },
    {
      factor: 1,
    }
  );
})();

@jsgan94
Copy link

jsgan94 commented Dec 1, 2019

var attempt = 0;

function asyncThing() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log("running");
      ++attempt;
      if (attempt < 3) reject("dogs");
      resolve(1000);
    }, 500);
  });
}

I don't see anything wrong, the function is always rejecting of course the console logs won't be running.

@sunshineo
Copy link

@robcalcroft fetch will throw only when a network error is encountered. 404 (or 403) are not a network error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants