So, for one second, the function goes to sleep, and after the promise is resolved, it will log the next time the same string, and then again goes to sleep for 1 second, and so on.ĭone! Yello, D'oh Yello, D'oh Yello, D'oh Yello, D'oh The task() function logs the Yello and D’oh values within 1 second. We defined an array list that can help create a loop. The keyword “ await” makes JavaScript wait until that promise settles and returns its result. Before a function, the word “ async” means one simple thing: a function always returns a promise. Here, we used async-await because sleep() will return the promise. In our code, that specific function is the task(). So, we will use the sleep() function in the task() function to sleep. Then, we will call the sleep() function when we need to sleep the specific function. In this example, first, we have defined a sleep function.
0 Comments
Leave a Reply. |