Retrying a request using 'request' library without extra modules

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












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.







share|improve this question



















  • 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
















up vote
1
down vote

favorite
1












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.







share|improve this question



















  • 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












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.







share|improve this question











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.









share|improve this question










share|improve this question




share|improve this question









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
















  • 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










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).






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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).






    share|improve this answer

























      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).






      share|improve this answer























        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).






        share|improve this answer













        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).







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 16 at 22:02









        jfriend00

        3,407718




        3,407718






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?