February 11, 2020
Typescript delay with async/await
Sometimes it is necessary in JavaScript to delay/sleep a program for a certain time. Therefore we can generate this behavior ourselves using a small helper function, thanks to the asynchronous nature of javascript. For example:
1 2 3 |
function sleep(ms: number) { return new Promise(resolve =>; setTimeout(resolve, ms)); } |
With this we can delay the program flow for a certain time in the following function
1 2 3 4 5 6 7 |
async function delayExample() { //Say Hello console.log('Hello'); // Say World after 2000 milliseconds await sleep(2000); console.log("World"); } |
But it […]
READ MORE