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).
3
  • 2
    Replace your promise.all with a for of. You can then also add a simple promise delay with await. Commented Oct 21, 2020 at 8: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. Commented Oct 21, 2020 at 8:45
  • note for self - check this one: stackoverflow.com/questions/24586110/… Commented Oct 21, 2020 at 9:49

2 Answers 2

1

This should work i guess

urls.map(x => {
     setTimeout(() => {
        axios.get(x);    
     }, 500);
});
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.