JAVASCRIPT-DEVELOPER-I practice question 12 of 30

Refer to the code below: 01 const exec = (item, delay) =>{ 02 newPromise(resolve => setTimeout( () => resolve(item), delay)), 03 async function…

Full set: 219 questions

Domain Asynchronous Programming

Refer to the code below: 01 const exec = (item, delay) =>{ 02 newPromise(resolve => setTimeout( () => resolve(item), delay)), 03 async function runParallel() { 04 Const (result1, result2, result3) = await Promise.all{ 05 [exec (‘x’, ‘100’) , exec(‘y’, 500), exec(‘z’, ‘100’)] 06 ); 07 return `parallel is done: $(result1)$(result2)$(result3)`; 08 } } } Which two statements correctly execute the runParallel () function? Choose 2 answers
Answer options
A: Async runParallel () .then(data);
B: runParallel ( ). done(function(data){return data;});
C: runParallel () .then(data); Correct answer
D: runParallel () .then(function(data)return data Correct answer
Correct Answer:
C. runParallel () .then(data);; D. runParallel () .then(function(data)return data
Explanation

The runParallel function is declared with the async keyword. In JavaScript, an async function always returns a Promise. The standard way to handle the resolved value of a Promise is by using the .then() method. This method takes a callback function as an argument, which is executed once the promise is fulfilled. Both options C and D correctly call the runParallel() function and use the .then() method to process the result. Option D provides an inline anonymous function as the callback. Option C passes a variable named data, which is also valid, assuming data is a reference to a function defined elsewhere in the scope.

Why Incorrect

A. Async runParallel () .then(data);
The async keyword is used to define a function, not to invoke it. This statement is a syntax error.
B. runParallel ( ). done(function(data){return data;});
The .done() method is not part of the standard ECMAScript Promise API, which is used by Salesforce Lightning Web Components and modern JavaScript.

References

1. MDN Web Docs (Mozilla): The async function declaration creates an AsyncFunction object. Each time when an async function is called, it returns a new Promise which will be resolved with the value returned by the async function.

Source: MDN Web Docs, "async function". (See the "Return value" section).

2. MDN Web Docs (Mozilla): The then() method of a Promise object takes up to two arguments: callback functions for the fulfilled and rejected cases of the Promise.

Syntax: p.then((value) => { / fulfillment handler / })

Source: MDN Web Docs, "Promise.prototype.then()". (See the "Syntax" and "Parameters" sections).

3. Salesforce Lightning Web Components Developer Guide: The guide demonstrates handling asynchronous Apex calls, which return promises, using the standard .then() and .catch() syntax.

Example: getContactList() .then(result => { ... }) .catch(error => { ... });

Source: Salesforce Lightning Web Components Developer Guide, "Call Apex Methods Imperatively".

4. ECMAScript® 2022 Language Specification (ECMA-262): The specification for JavaScript defines the methods available on the Promise prototype. The methods include then, catch, and finally, but do not include done.

Source: ECMA-262, 13th Edition, June 2022, Section 27.2.5 "Properties of the Promise Prototype Object".

Scroll to Top