How to do repeated requests until one succeeds without blocking in node?

前端 未结 7 2019
执笔经年
执笔经年 2021-01-31 17:35

I have a function that takes a parameter and a callback. It\'s supposed to do a request to a remote API and get some info based on the parameter. When it gets the info, it needs

7条回答
  •  再見小時候
    2021-01-31 18:04

    A library called Flashheart is also a suitable alternative. It's a rest client designed to be easy to use and supports retries.

    For example, configure Flashheart to retry 10 times, with a delay of 500ms between requests:

    const client = require('flashheart').createClient({
      retries: 10,
      retryTimeout: 500
    });
    
    const url = "https://www.example.com/";
    client.get(url, (err, body) => {
       if (err) {
          console.error('handle error: ', err);
          return;
       }
       console.log(body);
    });
    

    For further information, check out the docs: https://github.com/bbc/flashheart

    Disclaimer: I have contributed to this library.

提交回复
热议问题