1const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2
3(async () => {
4 console.log('1');
5 await delay(1000);
6 console.log('2');
7})();
1async function init() {
2 console.log(1);
3 await sleep(1000);
4 console.log(2);
5}
6
7function sleep(ms) {
8 return new Promise((resolve) => {
9 setTimeout(resolve, ms);
10 });
11}
12
13// one liner
14let sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
1//Javascipt is asyncronous so you can't pause/block code execution
2//You can delay doing something with setTimeout
3setTimeout(function(){
4 alert("Sup!");
5}, 2000);//wait 2 seconds
1const { promisify } = require('util');
2
3const delay = promisify(setTimeout);
4
5// Usage:
6(async () => {
7 console.log('1');
8 await delay(1000);
9 console.log('2');
10})();