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)
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
TypeScript: How to make code wait / sleep for few seconds before next step?
I've edited you example code using vanilla js and async/await,
you can implement it in Typescript as well
class App
{
async loadAPI(status) {
console.log(status)
if (false) {
}
else
{
await this.sleep(2000);
this.loadAPI(status);
}
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
(new App).loadAPI('loadAPI called')
Waiting for timeout to finish before executing next line of code
I have a theory that this is because the pausing method I used only pauses the async function but not entirely sure if that is correct.
Indeed. You need to mark the mergeSort
function as async
as well, so you can await
the merge()
as well as the two recursive mergeSort()
calls.
async function mergeSort(unsortedArray, aux = [...unsortedArray], lowIndex = 0, highIndex = unsortedArray.length - 1) { /*
^^^^^ */
if (highIndex === lowIndex) return;
const midIndex = Math.floor((highIndex + lowIndex) / 2);
await mergeSort(unsortedArray, aux, lowIndex, midIndex);
//^^^^^
await mergeSort(unsortedArray, aux, midIndex + 1, highIndex);
//^^^^^
await merge(unsortedArray, aux, lowIndex, midIndex, highIndex);
//^^^^^
}
How do I make my React file sleep for 5 seconds before continuing?
I am not quite sure if this would help in your case, but you can use this cool manual js sleep function:
const sleep = ms => new Promise(r => setTimeout(r, ms));
and then before the fetch, you can call it like:
await sleep(5000)
Related Topics
How to Add Dom Element Script to Head Section
HTML 5 <Audio> - Play File at Certain Time Point
Jquery .Animate() Stop Scrolling When User Scrolls Manually
How to Detect Overflow in Div Element
"Scroll" to the Very Right of a Long Text Input
How to Detect Support for the HTML5 "Download" Attribute
Browsers Automatically Evaluate Hex or Hsl Colors to Rgb When Setting via Element.Style.Background
How to Animate the Drawing of Text on a Web Page
How to Window.Scrollto() with a Smooth Effect
Get Element Stylesheet Style in JavaScript
Safari in iOS8 Is Scrolling Screen When Fixed Elements Get Focus
Change Image Based on Dropdown Using JavaScript
How Does Linux Emulator in JavaScript by Fabrice Bellard Work