1

I am trying to create a "sleep" function where I like to wait 10 seconds before continue with next command.

This doesn't work. No sleep is occuring. How can I put it nicely in a function like I do below "sleep(10000)" which is possible to call?

console.log("Hello");
sleep(10000);
console.log("World!");

function sleep(ms) {

    return new Promise(resolve => setTimeout(resolve, ms));
}
Andreas
  • 1,121
  • 4
  • 17
  • 34

1 Answers1

7

use then, so as to execute code once the promise has been resolved.

console.log("Hello");
sleep(10000).then(() => {
    console.log("World!");
})


function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
random
  • 7,756
  • 3
  • 19
  • 25