Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to run two functions one after the other (sequentially).
Javascript
function first() {
console.log('First');
}
function second() {
console.log('Second');
}
first();
[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using setTimeout or Promise here causes asynchronous behavior, not sequential direct calls.
Forgetting to add parentheses to call the function.
✗ Incorrect
Calling second() after first() runs the functions one after the other, which is sequential execution.
2fill in blank
mediumComplete the code to run two asynchronous functions in parallel using Promise.all.
Javascript
function task1() {
return new Promise(resolve => setTimeout(() => resolve('Task 1 done'), 1000));
}
function task2() {
return new Promise(resolve => setTimeout(() => resolve('Task 2 done'), 500));
}
Promise.[1]([task1(), task2()]).then(results => {
console.log(results);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Promise.race runs only the first promise that finishes.
Using then or catch directly on Promise does not run multiple promises.
✗ Incorrect
Promise.all runs multiple promises in parallel and waits for all to finish before continuing.
3fill in blank
hardFix the error in the async function to run tasks sequentially using await.
Javascript
async function runTasks() {
const result1 = task1();
const result2 = task2();
console.log(await result1);
console.log(await result2);
}
runTasks();
// Fix the line: const result1 = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using await causes the tasks to start but not wait for completion.
Using setTimeout does not return a promise and won't work with await.
✗ Incorrect
Using await before task1() ensures the function waits for task1 to finish before continuing, enabling sequential execution.
4fill in blank
hardFill both blanks to create a function that runs two tasks in parallel and logs their results.
Javascript
async function runParallel() {
const [result1, result2] = await Promise.[1]([task1(), task2()]);
console.log(result1);
console.log([2]);
}
runParallel(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Promise.race only waits for the first task to finish.
Logging result1 twice misses the second task's result.
✗ Incorrect
Promise.all runs tasks in parallel. Logging result1 and result2 shows both results after completion.
5fill in blank
hardFill all three blanks to create a function that runs tasks sequentially using await and logs results.
Javascript
async function runSequential() {
const result1 = await [1]();
console.log(result1);
const result2 = await [2]();
console.log([3]);
}
runSequential(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling tasks without await runs them in parallel.
Logging the wrong result variable causes confusion.
✗ Incorrect
Awaiting task1 and task2 runs them one after the other. Logging result1 and result2 shows their outputs in order.