Non-blocking sleep in an asynchronous function

If you would like to sleep the execution for a specific amount of time you could do it by the following ways:

const util = require('util');
const sleep = util.promisify(setTimeout);


const sleep = delay => new Promise(resolve => setTimeout(resolve, delay));

then in your code you could simply invoke it as:

await sleep(1000);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.