Sobes.tech
Middle+

/** * Write an asynchronous function * that will "sleep" for a specified number of milliseconds, * and then complete successfully */ function sleep(duration) { } // Example const startTime = Date.now(); console.log("Start sleeping..."); sleep(2000).then(() => { console.log("Woke up after 2 seconds!"); console.log("Time passed: ", Date.now() - startTime); }); sleep(1000).then(() => { console.log("Woke up after 1 second!"); console.log("Time passed: ", Date.now() - startTime); });

sobes.tech AI

Answer from AI

The sleep function should return a Promise that resolves after a specified number of milliseconds. This can be implemented using setTimeout inside a Promise:

function sleep(duration) {
    return new Promise(resolve => {
        setTimeout(resolve, duration);
    });
}

The example usage from the question will work correctly, displaying messages after the specified wait time.

/** * Write an asynchronous function * that will… - sobes.tech