Complete the code to log messages synchronously.
console.log('Start'); console.log([1]); console.log('End');
The synchronous code logs 'Start', then 'Middle', then 'End' immediately in order.
Complete the code to log 'Middle' asynchronously after 1 second.
console.log('Start'); [1]; console.log('End');
setTimeout delays the 'Middle' message by 1 second, making it asynchronous.
Fix the error in the asynchronous function to log 'Done'.
async function task() {
await [1];
console.log('Done');
}
task();await expects a Promise. Promise.resolve() returns a resolved Promise immediately.
Fill both blanks to create a Promise that resolves after 1 second and logs 'Finished'.
const wait = () => new Promise(([1], [2]) => { setTimeout(resolve, 1000); }); wait().then(() => console.log('Finished'));
The Promise constructor takes two functions: resolve and reject. We call resolve after 1 second.
Fill all three blanks to create an async function that waits 2 seconds before logging 'All done!'.
function delay(ms) {
return new Promise(([1], [2]) => {
setTimeout([3], ms);
});
}
async function run() {
await delay(2000);
console.log('All done!');
}
run();The Promise constructor uses resolve and reject. setTimeout calls resolve after the delay.