sleep.js 333 B

1234567891011121314151617181920
  1. /**
  2. * Example of a sleep() function without any extra packages.
  3. *
  4. * @source https://stackoverflow.com/a/41957152
  5. */
  6. async function init() {
  7. console.log('before sleep');
  8. await sleep(1000);
  9. console.log('after sleep');
  10. }
  11. function sleep(ms) {
  12. return new Promise((resolve) => {
  13. setTimeout(resolve, ms);
  14. });
  15. }
  16. init();