Next.js Discord

Discord Forum

How to simulate a wait time

Answered
WhyFencePost (Ping Reply) posted this in #help-forum
Open in Discord
How would i simulate a wait time in one of my form actions (It would need to do data fetching, and so needs to wait), and I want to simulate that while I am just checking the code.
setTimeout(3000) fails, as it says that it cant have any args
Answered by Sun bear
Hi, I do it like this:

await new Promise(resolve => setTimeout(resolve, 3000));


The function of course has to be async in which you use it
View full answer

9 Replies

Sun bear
Hi, I do it like this:

await new Promise(resolve => setTimeout(resolve, 3000));


The function of course has to be async in which you use it
Answer
thanks
what if its in a useeffect
Sun bear
Cant you just use set timeout there?

setTimeout(() => {
console.log("waiting...")
},3000)
i might be able to, ill check
yea, ig
/**
 *  **Dont use production unless you know what you are doing. On server cost is calculated by computation time!**
 * @description
 *  **Function to add Fake delays.**
 * @param ms number
 * @returns nothing.
 * @example await sleep(2000) // Fake delay of 2 seconds
 *
 */
export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
thanks mate
always!