JavaScript - Async sleep function
Define the async function
const sleep = function(ms) {
return new Promise(function(res) {
setTimeout(function() {
res();
}, ms);
});
};
Using the async function
(async function() {
while (true) {
console.log(new Date());
await sleep(1000);
}
})();