Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda