Created
February 28, 2020 16:07
-
-
Save ikbelkirasan/3d1525fdc23aa08c98125d1fe2f7c7e8 to your computer and use it in GitHub Desktop.
Retry a function call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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