Middle
// create a function to wait 1000 with passing control outside // parameterize
sobes.tech AI
Answer from AI
You can create a function that returns a Promise which resolves after a specified delay, while allowing control to return to the event loop, enabling other operations to run.
Here's an example in JavaScript with a parameterized delay:
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Usage:
async function example() {
console.log('Starting');
await wait(1000); // waits 1000 ms, control returns to the event loop
console.log('One second passed');
}
example();
This approach prevents blocking the execution thread and allows asynchronous waiting with control passed to other tasks.