Question 32
Prashant and Nikita are creating a quiz question paper collaboratively. They are each working on different sections — and complete their tasks in parallel. Meanwhile, Mayur is enjoying a single- player game that finishes when the game loop ends.
To compare productivity:
● If Prashant and Nikita (together) finish faster than Mayur, the winner is "Team".
● If Mayur finishes faster, he wins.
● If both take the same time, Mayur wins by default.
You are simulating this scenario in the browser.Prashant and Nikita are represented using parallel Promises. Mayur is represented using a single Promise. The frontend must calculate who finishes first using asynchronous logic, and display the result.
app.js
function simulateTasks() { const prashant = new Promise(resolve => setTimeout(() =>resolve("Prashant"), 3000)); const nikita = new Promise(resolve => setTimeout(() =>resolve("Nikita"), 2000)); const mayur = new Promise(resolve => setTimeout(() =>resolve("Mayur"), 4000));
const team = Promise.all([prashant, nikita]).then(() =>"Team");
Promise.race([team, mayur]).then(winner => { console.log("Winner is:", winner); });}Based on the above data, answer the given subquestions.
app2.js
async function playGame() { const start = Date.now();
const prashantPromise = new Promise(resolve => setTimeout(resolve, 3000)); const nikitaPromise = new Promise(resolve => setTimeout(resolve, 2000)); const mayurPromise = new Promise(resolve => setTimeout(resolve, 2000));
await Promise.all([prashantPromise, nikitaPromise]); const teamTime = Date.now() - start;
await mayurPromise; const mayurTime = Date.now() - start;
const winner = (teamTime < mayurTime) ? "Team" : "Mayur"; console.log(`Winner is: ${winner}`);}The Team takes 5 seconds in total. Mayur takes 2 seconds. According to the logic of app2.js, who is the winner?
Mayur
Team
Undefined
Error due to async logic