Sometimes, we want to use setTimeout on promise chain with JavaScript.
In this article, we’ll look at how to use setTimeout on promise chain with JavaScript.
How to use setTimeout on promise chain with JavaScript?
To use setTimeout on promise chain with JavaScript, we can create a promise that calls setTimeout
.
For instance, we write
await new Promise((resolve) => setTimeout(resolve, 1000));
to create a promise with the Promise
constructor by calling it with a callback that calls setTimeout
.
And then we use resolve
as the callback for setTimeout
.
Then we use await
to wait for the promise to resolve before running the next line of code.
Conclusion
To use setTimeout on promise chain with JavaScript, we can create a promise that calls setTimeout
.