Skip to content

Instantly share code, notes, and snippets.

@ikbelkirasan
Created February 28, 2020 16:07
Show Gist options
  • Save ikbelkirasan/3d1525fdc23aa08c98125d1fe2f7c7e8 to your computer and use it in GitHub Desktop.
Save ikbelkirasan/3d1525fdc23aa08c98125d1fe2f7c7e8 to your computer and use it in GitHub Desktop.
Retry a function call
const retry = require("retry");
const sinon = require("sinon");
const sendRequest = sinon.stub();
sendRequest.onFirstCall().rejects(new Error("yaw kiw 1"));
sendRequest.onSecondCall().rejects(new Error("yaw kiw 2"));
sendRequest.onThirdCall().resolves({ message: "it works" });
const operation = retry.operation({
retries: 2,
factor: 3,
minTimeout: 1 * 1000,
maxTimeout: 60 * 1000,
randomize: true
});
operation.attempt(async currentAttempt => {
console.log("sending request: ", currentAttempt, " attempt");
try {
const response = await sendRequest({
method: "GET",
url: "http://example.com"
});
response; //?
} catch (e) {
e; //?
if (operation.retry(e)) {
return;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment