This is the cleanest and most modern approach to handle a delay in Node.js. It makes use of async/await along with a Promise that resolves after a delay:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
console.log('Welcome to my console,');
await sleep(10000); // Wait for 10 seconds
console.log('Blah blah blah blah extra-blah');
}
main();