0

I am expanding my pet project (twitterwatcher.com/c/) with alerts, for that, I need to get a bit more details from the binance API, long story short I am receiving an error: 509, Bandwidth Limit Exceeded

I guess that I am sending the requests to the binance in a way to rapid manner - and I have to introduce a form of delay into my axios requests. And now here comes the problem - as my code is doing it automatically - how can I add the delays?

below the JS code:

let urls = ['A', 'B', 'C'] //this id dynamic 
let output = null; 
const responses = await axios.all(urls.map(x => axios.get(x)));
if(Array.isArray(responses)){
        output = []; 
        responses.forEach(wip => {wip.data.forEach((element) => {
            output.push({
                //push the JSON in
            });
        });
    })
}else{
    output = []; 
    outputWiP.data.forEach((element) => {
            output.push({
                //push the JSON in
            });
        });
    })
}

And a question:

  • How can I introduce a 0.5s delay without major changes in the logic? I've seen solutions with .then() approach, I do not want to have a unique implementation in this piece of code (I am using async/await wherever I can).
tzim
  • 1,715
  • 15
  • 37
  • 2
    Replace your promise.all with a for of. You can then also add a simple promise delay with await. – Keith Oct 21 '20 at 08:27
  • @Keith will try, thank you. I was hoping for an extra "hidden" param in the get configuration that can enforce the delay. I guess this is not that common case for it to be there. – tzim Oct 21 '20 at 08:45
  • note for self - check this one: https://stackoverflow.com/questions/24586110/resolve-promises-one-after-another-i-e-in-sequence/41115086#41115086 – tzim Oct 21 '20 at 09:49

2 Answers2

1

This should work i guess

urls.map(x => {
     setTimeout(() => {
        axios.get(x);    
     }, 500);
});
Aabir Hussain
  • 1,161
  • 1
  • 11
  • 29
  • 1
    You do know that this will just delay all urls for half a second? IOW: all requests are still going to happen at the same time, just half a second later.. – Keith Oct 21 '20 at 10:41
0

I used this approach. Works.

/*
 * serial executes Promises sequentially.
 * @param {funcs} An array of funcs that return promises.
 * @example
 * const urls = ['/url1', '/url2', '/url3']
 * serial(urls.map(url => () => $.ajax(url)))
 *     .then(console.info.bind(console)) 
 */
const serial = funcs =>
    funcs.reduce((promise, func) =>
        promise.then(
            result => func().then(Array.prototype.concat.bind(result))), 
            Promise.resolve([]))

solution from Resolve promises one after another (i.e. in sequence)?

IMPORTANT: It is not with a delay, BUT sequentional.

tzim
  • 1,715
  • 15
  • 37