Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Is there a way to make my code wait a number of seconds before executing the next piece of code? (API Requests Per Second Limit)

setTimeout() doesn't seem to work, and I've read up on why and I understand. Something about it being asynchronous, so it doesn't delay the other functions in your code from running. So how can I make it so that it waits a few seconds before sending out another axios.request? And the reason I'm asking about this is because the API im requesting only allows one request per second.

setTimeout(function () {
            axios.request(options).then(function (response) {
                for (let i = 0, l = response.data["result"].length; i < l; i++) {

                    var obj = response.data.result;
                    
                    
                    console.log(response.data.result[i].hash);
                }
                options.params.term = obj[1].hash
                options.params.func = 'dehash'
                setTimeout(function () {
                    axios.request(options).then(function (response2) {
                        
                        message.reply(termargs + " passwords:" + '`' + JSON.stringify(response2.data.found) + '`');
                    });
                }, 1250);
                options.params.term = obj[2].hash
                setTimeout(function () {
                    axios.request(options).then(function (response3) {
                        
                        message.reply(termargs + " passwords:" + '`' + JSON.stringify(response3.data.found) + '`');
                    });
                }, 1250);
                options.params.term = obj[3].hash
                setTimeout(function () {
                    axios.request(options).then(function (response4) {
                        
                        message.reply(termargs + " passwords:" + '`' + JSON.stringify(response4.data.found) + '`');
                    });
                }, 1250); 
               

            }).catch(function (error) {
                message.reply("There was an error!" + '`' + 'Couldnt find user.' + '`');
                console.log(error);
            });
            }, 1250);

I'm going to reiterate the fact that I'm new to Javascript, and this no wait function thing really makes my toes curl, as I use it all the time in the other programming language called Lua.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Once you are running async functions you will need to use async/await to do that, setTimeout/Sleep wont work.

if you want a request to end when going to the next code you can do it this way:

async function funcName(){
 const result = await axios.request(options).then(function (response2) {
  message.reply(termargs + " passwords:" + '`' + 
  JSON.stringify(response2.data.found) + '`');
 });

 const result2 = await axios.request(options).then(function (response2) {
  message.reply(termargs + " passwords:" + '`' + 
  JSON.stringify(response2.data.found) + '`');
 });
}

funcName();

on this example first he will end result 1, than go to solve result 2...

Here is the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

about 4 years ago · Juan Pablo Isaza Report

0

If you want to use setTimeout, you should look after the thread i have linked this thread.

If it doesn't work for you, you could also try the sleep method.

sleep(ms).then(() => {

})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!