0
0
Javascriptprogramming~10 mins

Sequential vs parallel execution in Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Asecond
BsetTimeout
CPromise
Dasync
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.
2fill in blank
medium

Complete 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'
Arace
Bcatch
Cthen
Dall
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.
3fill in blank
hard

Fix 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'
AsetTimeout(task1, 1000)
Bawait task1()
Ctask1()
DPromise.resolve(task1())
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.
4fill in blank
hard

Fill 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'
Aall
Bresult2
Crace
Dresult1
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.
5fill in blank
hard

Fill 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'
Atask1
Btask2
Cresult2
Dresult1
Attempts:
3 left
💡 Hint
Common Mistakes
Calling tasks without await runs them in parallel.
Logging the wrong result variable causes confusion.