Retrying a request using 'request' library without extra modules
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am using the request module and wanted to implement some retries for robustness. It seems excessive to use a whole new or extra module just to do a retries so I've put together the following as an exercise to retry requests using only the request
library:
var checkUrl = function(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
var request = require('request');
request(url, async function (error, response, body)
if (error)
if (retriesLeft > 0)
console.log(url + ' : not available yet');
await sleep(timeoutMS);
checkUrl(url, --retriesLeft);
else
console.log(url + ' : URL is available');
);
function sleep(ms)
return new Promise(resolve => setTimeout(resolve, ms));
checkUrl('http://www.example.com/', 3, 5000);
Is this a reasonable approach? It seems to work. It's just a basic example - I've not considered redirects for instance.
Of course this can be wrapped up to make it easier to use and then, ironically, becomes a sort of module but it would not require pulling anything external.
javascript promise
add a comment |Â
up vote
1
down vote
favorite
I am using the request module and wanted to implement some retries for robustness. It seems excessive to use a whole new or extra module just to do a retries so I've put together the following as an exercise to retry requests using only the request
library:
var checkUrl = function(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
var request = require('request');
request(url, async function (error, response, body)
if (error)
if (retriesLeft > 0)
console.log(url + ' : not available yet');
await sleep(timeoutMS);
checkUrl(url, --retriesLeft);
else
console.log(url + ' : URL is available');
);
function sleep(ms)
return new Promise(resolve => setTimeout(resolve, ms));
checkUrl('http://www.example.com/', 3, 5000);
Is this a reasonable approach? It seems to work. It's just a basic example - I've not considered redirects for instance.
Of course this can be wrapped up to make it easier to use and then, ironically, becomes a sort of module but it would not require pulling anything external.
javascript promise
It's not excessive to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
â jfriend00
Jul 16 at 6:10
@TobySpeight - OK, I put that into an answer. I didn't consider it an answer at the time because I didn't have a specific library recommendation in mind to illustrate, but I just now had the time to go look that up and incorporate it into an answer.
â jfriend00
Jul 16 at 22:03
Message received! I shall accept the provided answer.
â Fred Clausen
Jul 17 at 0:39
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using the request module and wanted to implement some retries for robustness. It seems excessive to use a whole new or extra module just to do a retries so I've put together the following as an exercise to retry requests using only the request
library:
var checkUrl = function(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
var request = require('request');
request(url, async function (error, response, body)
if (error)
if (retriesLeft > 0)
console.log(url + ' : not available yet');
await sleep(timeoutMS);
checkUrl(url, --retriesLeft);
else
console.log(url + ' : URL is available');
);
function sleep(ms)
return new Promise(resolve => setTimeout(resolve, ms));
checkUrl('http://www.example.com/', 3, 5000);
Is this a reasonable approach? It seems to work. It's just a basic example - I've not considered redirects for instance.
Of course this can be wrapped up to make it easier to use and then, ironically, becomes a sort of module but it would not require pulling anything external.
javascript promise
I am using the request module and wanted to implement some retries for robustness. It seems excessive to use a whole new or extra module just to do a retries so I've put together the following as an exercise to retry requests using only the request
library:
var checkUrl = function(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
var request = require('request');
request(url, async function (error, response, body)
if (error)
if (retriesLeft > 0)
console.log(url + ' : not available yet');
await sleep(timeoutMS);
checkUrl(url, --retriesLeft);
else
console.log(url + ' : URL is available');
);
function sleep(ms)
return new Promise(resolve => setTimeout(resolve, ms));
checkUrl('http://www.example.com/', 3, 5000);
Is this a reasonable approach? It seems to work. It's just a basic example - I've not considered redirects for instance.
Of course this can be wrapped up to make it easier to use and then, ironically, becomes a sort of module but it would not require pulling anything external.
javascript promise
asked Jul 16 at 5:50
Fred Clausen
1084
1084
It's not excessive to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
â jfriend00
Jul 16 at 6:10
@TobySpeight - OK, I put that into an answer. I didn't consider it an answer at the time because I didn't have a specific library recommendation in mind to illustrate, but I just now had the time to go look that up and incorporate it into an answer.
â jfriend00
Jul 16 at 22:03
Message received! I shall accept the provided answer.
â Fred Clausen
Jul 17 at 0:39
add a comment |Â
It's not excessive to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
â jfriend00
Jul 16 at 6:10
@TobySpeight - OK, I put that into an answer. I didn't consider it an answer at the time because I didn't have a specific library recommendation in mind to illustrate, but I just now had the time to go look that up and incorporate it into an answer.
â jfriend00
Jul 16 at 22:03
Message received! I shall accept the provided answer.
â Fred Clausen
Jul 17 at 0:39
It's not excessive to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
â jfriend00
Jul 16 at 6:10
It's not excessive to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
â jfriend00
Jul 16 at 6:10
@TobySpeight - OK, I put that into an answer. I didn't consider it an answer at the time because I didn't have a specific library recommendation in mind to illustrate, but I just now had the time to go look that up and incorporate it into an answer.
â jfriend00
Jul 16 at 22:03
@TobySpeight - OK, I put that into an answer. I didn't consider it an answer at the time because I didn't have a specific library recommendation in mind to illustrate, but I just now had the time to go look that up and incorporate it into an answer.
â jfriend00
Jul 16 at 22:03
Message received! I shall accept the provided answer.
â Fred Clausen
Jul 17 at 0:39
Message received! I shall accept the provided answer.
â Fred Clausen
Jul 17 at 0:39
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
It's not "excessive" (your term) to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
I didn't do an exhaustive search, but I came across requestretry on NPM that seems to be pretty much what you might want (there are several other similar options on NPM too). Using it, reduces your code to this:
const requestRetry = require('requestretry');
function checkUrl(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
return requestRetry(url: url, maxAttempts: retriesLeft + 1, retryDelay: timeoutMS);
checkUrl('http://www.example.com/', 3, 5000).then(data =>
console.log(data);
).catch(err =>
console.log(err);
);
This particular library even has ways for you to plug in your own retry strategy or delay strategy (if, for example, you wanted to implement a back-off strategy to the retries so the retry interval gets longer after more failures which is sometimes desirable).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It's not "excessive" (your term) to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
I didn't do an exhaustive search, but I came across requestretry on NPM that seems to be pretty much what you might want (there are several other similar options on NPM too). Using it, reduces your code to this:
const requestRetry = require('requestretry');
function checkUrl(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
return requestRetry(url: url, maxAttempts: retriesLeft + 1, retryDelay: timeoutMS);
checkUrl('http://www.example.com/', 3, 5000).then(data =>
console.log(data);
).catch(err =>
console.log(err);
);
This particular library even has ways for you to plug in your own retry strategy or delay strategy (if, for example, you wanted to implement a back-off strategy to the retries so the retry interval gets longer after more failures which is sometimes desirable).
add a comment |Â
up vote
1
down vote
accepted
It's not "excessive" (your term) to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
I didn't do an exhaustive search, but I came across requestretry on NPM that seems to be pretty much what you might want (there are several other similar options on NPM too). Using it, reduces your code to this:
const requestRetry = require('requestretry');
function checkUrl(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
return requestRetry(url: url, maxAttempts: retriesLeft + 1, retryDelay: timeoutMS);
checkUrl('http://www.example.com/', 3, 5000).then(data =>
console.log(data);
).catch(err =>
console.log(err);
);
This particular library even has ways for you to plug in your own retry strategy or delay strategy (if, for example, you wanted to implement a back-off strategy to the retries so the retry interval gets longer after more failures which is sometimes desirable).
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It's not "excessive" (your term) to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
I didn't do an exhaustive search, but I came across requestretry on NPM that seems to be pretty much what you might want (there are several other similar options on NPM too). Using it, reduces your code to this:
const requestRetry = require('requestretry');
function checkUrl(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
return requestRetry(url: url, maxAttempts: retriesLeft + 1, retryDelay: timeoutMS);
checkUrl('http://www.example.com/', 3, 5000).then(data =>
console.log(data);
).catch(err =>
console.log(err);
);
This particular library even has ways for you to plug in your own retry strategy or delay strategy (if, for example, you wanted to implement a back-off strategy to the retries so the retry interval gets longer after more failures which is sometimes desirable).
It's not "excessive" (your term) to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
I didn't do an exhaustive search, but I came across requestretry on NPM that seems to be pretty much what you might want (there are several other similar options on NPM too). Using it, reduces your code to this:
const requestRetry = require('requestretry');
function checkUrl(url = 'http://localhost', retriesLeft = 0, timeoutMS = 3000)
return requestRetry(url: url, maxAttempts: retriesLeft + 1, retryDelay: timeoutMS);
checkUrl('http://www.example.com/', 3, 5000).then(data =>
console.log(data);
).catch(err =>
console.log(err);
);
This particular library even has ways for you to plug in your own retry strategy or delay strategy (if, for example, you wanted to implement a back-off strategy to the retries so the retry interval gets longer after more failures which is sometimes desirable).
answered Jul 16 at 22:02
jfriend00
3,407718
3,407718
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199568%2fretrying-a-request-using-request-library-without-extra-modules%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It's not excessive to use a pre-made module that does what you want. In fact, if it's solid code, then it's smart to use something that's already written and tested and maintained by someone else. There's little cost on a server to using a small module that does what you want.
â jfriend00
Jul 16 at 6:10
@TobySpeight - OK, I put that into an answer. I didn't consider it an answer at the time because I didn't have a specific library recommendation in mind to illustrate, but I just now had the time to go look that up and incorporate it into an answer.
â jfriend00
Jul 16 at 22:03
Message received! I shall accept the provided answer.
â Fred Clausen
Jul 17 at 0:39