node wait 10 seconds
// Importing the built-in Node.js 'util' module
const { promisify } = require('util');
// Using the 'promisify' function from the 'util' module to convert 'setTimeout' to a promise-based function
const wait = promisify(setTimeout);
// Async function to wait for 10 seconds
async function waitForTenSeconds() {
console.log('Waiting for 10 seconds...');
await wait(10000); // Waiting for 10 seconds using the 'wait' function
console.log('Finished waiting for 10 seconds!');
}
// Calling the async function to initiate the 10-second wait
waitForTenSeconds();
Explanation:
- Import the 'util' module provided by Node.js.
- Destructure the 'promisify' function from the 'util' module, which will help convert the 'setTimeout' function into a promise-based function.
- Create a 'wait' function using 'promisify' that will return a promise-based version of 'setTimeout'.
- Define an async function named 'waitForTenSeconds()' which will handle the waiting logic.
- Log a message indicating the start of the 10-second wait using
console.log
. - Use the 'wait' function (which is a promisified version of 'setTimeout') to pause the execution for 10 seconds (10000 milliseconds) using
await
. - After the 10-second delay, log a message indicating the completion of the wait using
console.log
. - Call the 'waitForTenSeconds()' function to initiate the 10-second wait when the script is executed.