0

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.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
quiving
  • 23
  • 4

2 Answers2

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(() => {

})
robni
  • 904
  • 2
  • 6
  • 20
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