Salesforce certification · Exam Salesforce-JavaScript-Developer

Salesforce-JavaScript-Developer Mock Exam

Salesforce JavaScript Developer - JS-Dev-101

Free Salesforce-JavaScript-Developer questions on Asynchronous Programming, each with the answer and an explanation.

20 questions · 33 minutes · free, no sign-up

Asynchronous Programming questions

1–5 of 5 free
Question 1 Choose 1
Refer to the code below:

01 async function functionUnderTest(isOK) {

02 if (isOK) return ' OK ' ;

03 throw new Error( ' not OK ' );

04 }

Which assertion accurately tests the above code?
Show answerHide answer

Correct answer: A console.assert(await functionUnderTest(true), ' OK ' )

Explanation

Explanation:
This question tests your understanding of testing asynchronous functions using console.assert() with async/await syntax. The functionUnderTest is an async function that returns 'OK' when called with true and throws an error when called with false. To test the successful path, we need to call it with true, await its resolution, and assert that the resolved value matches 'OK'. The correct syntax for console.assert() is console.assert(condition, message) – it logs an error if the condition is falsy, but does nothing if truthy.

Correct Option:

A. console.assert(await functionUnderTest(true), 'OK') –
Correct. This calls functionUnderTest(true) which resolves to 'OK'. The await keyword extracts the resolved value 'OK', which is a truthy string. Since the condition is truthy, console.assert() does nothing (passes silently). The second argument 'OK' is the message that would be logged only if the condition were falsy. This accurately tests that the function returns a truthy value when called with true.

Incorrect Options:

B. console.assert(await (functionUnderTest(true), 'not OK')) –
Incorrect. This uses the comma operator inside parentheses. The expression (functionUnderTest(true), 'not OK') evaluates both operands and returns the last one, which is the string 'not OK'. The await then resolves to 'not OK' (a truthy string), so the assertion passes, but it does not test the actual return value of functionUnderTest. This is a misleading and incorrect test.

C. console.assert(functionUnderTest(true), 'OK') –
Incorrect. This calls functionUnderTest(true) without await, so it returns a Promise object, not the resolved value. A Promise object is truthy, so the assertion passes, but it does not verify that the function resolved to 'OK'. This tests the wrong thing – it only checks that a Promise was returned, not its fulfillment value.

D. console.assert(await functionUnderTest(true), 'not OK') –
Incorrect. While the syntax is valid and the condition 'OK' is truthy, the message 'not OK' is misleading and incorrect for a passing test. The assertion passes, but the message does not accurately describe what is being tested. When the condition is truthy, the message is never displayed anyway, so this is functionally equivalent to A in terms of behavior, but the message should be descriptive of the expected outcome for clarity.

Reference:

MDN Web Docs – console.assert() method

MDN Web Docs – async/await syntax

MDN Web Docs – Promise and asynchronous functions

Salesforce Trailhead – JavaScript Essentials: Asynchronous Programming and Testing
Question 2 Choose 2
Refer to the code:

01 const exec = (item, delay) = >

02 new Promise(resolve = > setTimeout(() = > resolve(item), delay));

03

04 async function runParallel() {

05 const [result1, result2, result3] = await Promise.all(

06 [exec( ' x ' , ' 100 ' ), exec( ' y ' , ' 500 ' ), exec( ' z ' , ' 100 ' )]

07 );

08 return `parallel is done: ${result1}${result2}${result3}`;

09 }

Which two statements correctly execute runParallel()?
Show answerHide answer

Correct answer: B, D runParallel().then(function(data){

return data;

});
runParallel().then(data);

Explanation

Explanation:
This question tests your understanding of executing asynchronous functions and handling promises in JavaScript. The runParallel() function is declared as async, which means it returns a Promise. To execute it and access the resolved value, you must call it and handle the returned promise using .then() with a callback function. The .then() method expects a function as its argument, and data (the resolved value) is passed to that callback. The syntax must be correct for the promise chain to work.

Correct Options:

B. runParallel().then(function(data){ return data; }); –
Correct. This correctly calls runParallel(), which returns a Promise. The .then() method is called with a proper callback function that receives the resolved value (data). The callback returns the data, which could be used in further chaining. This is valid and standard promise handling syntax.

D. runParallel().then(data); –
Correct. In JavaScript, .then() accepts a function reference. Since data is a variable (presumably defined elsewhere as a function), passing data directly to .then() is valid as long as data is a function. In the context of the question, this implies data is a defined callback function. This is a concise way to pass a function reference to .then().

Incorrect Options:

A. async runParallel().then(data); –
Incorrect. The async keyword is used only when declaring a function, not when calling it. Placing async before a function call is a syntax error. The correct call is simply runParallel().then(data);. The async keyword is already part of the function declaration (line 04), so it should not be repeated during invocation.

C. runParallel().done(function(data){ return data; }); –
Incorrect. There is no .done() method on a Promise in standard JavaScript. While some older libraries (like jQuery) have a .done() method, native JavaScript promises only support .then(), .catch(), and .finally(). This would throw a TypeError because .done() does not exist.

Reference:

MDN Web Docs – async function and return value

MDN Web Docs – Promise.prototype.then() method

MDN Web Docs – Using promises and chaining

MDN Web Docs – async/await syntax and execution

Salesforce Trailhead – JavaScript Essentials: Asynchronous Programming and Promises
Question 3 Choose 1
static delay = async delay = > {

return new Promise(resolve = > {

setTimeout(resolve, delay);

});

};

static asyncCall = async () = > {

await delay(1000);

console.log(1);

};

console.log(2);

asyncCall();

console.log(3);

Assume delay and asyncCall are in scope as functions.

What is logged to the console?
Show answerHide answer

Correct answer: D 2 3 1

Explanation

Explanation:
This question tests your understanding of asynchronous JavaScript, specifically the event loop, setTimeout, and async/await behavior. When the code executes, synchronous code runs first. The console.log(2) on line 10 executes immediately, logging 2. Then asyncCall() is invoked, which calls delay(1000), scheduling a timer that will resolve after 1000ms. However, await pauses the execution of asyncCall() but does not block the main thread. The synchronous console.log(3) on line 12 executes immediately after asyncCall() is called, logging 3. After the timer completes (1000ms later), the promise resolves, and console.log(1) is logged. Therefore, the order is 2, 3, 1.

Correct Option:

D. 2 3 1 – Correct. Let's trace the execution step by step:

Line 10: console.log(2) executes synchronously → logs 2.

Line 11: asyncCall() is invoked. Inside asyncCall, await delay(1000) is called.

delay(1000) schedules a setTimeout for 1000ms and returns a pending Promise.

The await keyword pauses the execution of asyncCall and returns control to the caller.

Line 12: console.log(3) executes synchronously → logs 3 (while the timer is still pending).

After 1000ms, the setTimeout callback resolves the promise, and asyncCall resumes.

console.log(1) executes → logs 1.

Final output order: 2, 3, 1.

Incorrect Options:

A. 1 2 3 –
Incorrect. This would imply that console.log(1) executes before console.log(2), but console.log(2) is synchronous and runs immediately. The asyncCall() function is asynchronous and its console.log(1) is delayed by the setTimeout, so it cannot appear before 2.

B. 1 3 2 –
Incorrect. This incorrectly places 1 first, but 1 is delayed by the 1000ms timer and cannot log before the synchronous 2 and 3. The synchronous code always executes before asynchronous callbacks.

C. 2 1 3 –
Incorrect. This incorrectly places 1 before 3. The await inside asyncCall pauses the function but does not block the main thread, so console.log(3) executes immediately after asyncCall() is invoked, before the timer resolves. Therefore, 3 must appear before 1.

Reference:

MDN Web Docs – async/await and the event loop

MDN Web Docs – setTimeout and asynchronous callbacks

MDN Web Docs – Promise and microtask/task queue

Salesforce Trailhead – JavaScript Essentials: Asynchronous Programming and Event Loop
Question 4 Choose 1
A developer has an isDeg function that takes one argument, pts. They want to schedule the function to run every minute.

What is the correct syntax for scheduling this function?
Show answerHide answer

Correct answer: B setInterval(isDeg, 60000, ' cat ' );

Explanation

Explanation:
This question tests your knowledge of scheduling functions in JavaScript using setInterval() and setTimeout(), specifically how to pass arguments to the scheduled function. The setInterval() method executes a function repeatedly at specified intervals. When you need to pass arguments to the callback function, you can provide them as additional parameters after the delay. The correct syntax is setInterval(function, delay, arg1, arg2, ...). Passing isDeg('cat') directly would execute the function immediately and pass its return value to setInterval(), which is incorrect.

Correct Option:

B. setInterval(isDeg, 60000, 'cat'); –
Correct. This syntax correctly schedules the isDeg function to run every 60,000 milliseconds (1 minute). The additional argument 'cat' is passed as an argument to isDeg each time it is invoked. This is the standard and correct way to schedule a function with arguments using setInterval().

Incorrect Options:

A. setInterval(isDeg('cat'), 60000); –
Incorrect. This calls isDeg('cat') immediately and passes its return value to setInterval() as the function to execute. Unless isDeg returns another function, this will not work as intended. If isDeg returns undefined or some other value, setInterval will attempt to call that value as a function, resulting in an error.

C. setTimeout(isDeg, 60000, 'cat'); –
Incorrect. While this syntax is valid for setTimeout(), the requirement is to run the function every minute. setTimeout() executes the function only once after the delay, not repeatedly. This does not meet the requirement of running the function every minute.

D. setTimeout(isDeg('cat'), 60000); –
Incorrect. This combines two errors: it calls isDeg('cat') immediately (like option A) and uses setTimeout() which only runs once. The function would execute immediately, and setTimeout() would receive its return value, which is not the intended behavior. It neither runs repeatedly nor schedules the function correctly.

Reference:

MDN Web Docs – setInterval() method and additional arguments

MDN Web Docs – setTimeout() method (single execution)

MDN Web Docs – Scheduling functions and passing arguments

Salesforce Trailhead – JavaScript Essentials: Timers and Asynchronous Scheduling
Question 5 Choose 1
Refer to the code:

01 function execute() {

02 return new Promise((resolve, reject) = > reject());

03 }

04 let promise = execute();

05

06 promise

07 .then(() = > console.log( ' Resolved1 ' ))

08 .then(() = > console.log( ' Resolved2 ' ))

09 .then(() = > console.log( ' Resolved3 ' ))

10 .catch(() = > console.log( ' Rejected ' ))

11 .then(() = > console.log( ' Resolved4 ' ));

What is the result when the Promise in the execute function is rejected?
Show answerHide answer

Correct answer: D Rejected Resolved4

Explanation

Explanation:
This question tests your understanding of promise chaining and error handling with catch(). The execute() function returns a Promise that immediately rejects (calls reject()). The initial promise chain starts with .then() handlers for the resolved case, but since the promise rejects, all .then() handlers before the first .catch() are skipped. The .catch() handler executes and logs 'Rejected'. After .catch(), the chain continues with .then() because a .catch() returns a new resolved promise by default (unless it throws an error). Therefore, 'Resolved4' is logged after 'Rejected'. The final output is 'Rejected' followed by 'Resolved4'.

Correct Option:

D. Rejected Resolved4 – Correct. Let's trace the execution:

execute() returns a Promise that rejects immediately.

The first .then() on line 07 is skipped because the promise is rejected.

The second .then() on line 08 is skipped.

The third .then() on line 09 is skipped.

The .catch() on line 10 catches the rejection and logs 'Rejected'.

The .catch() returns a resolved promise by default (no error thrown), so the chain continues.

The final .then() on line 11 executes and logs 'Resolved4'.

Final output: Rejected Resolved4.

Incorrect Options:

A. Resolved1 Resolved2 Resolved3 Rejected Resolved4 –
Incorrect. The initial promise rejects, so none of the .then() handlers before the .catch() execute. This option incorrectly assumes the .then() handlers run before the .catch(). They are skipped entirely.

B. Rejected –
Incorrect. This option ignores the final .then() after the .catch(). Once the .catch() handles the rejection, it returns a new resolved promise, allowing the subsequent .then() to execute. The chain does not stop after .catch() unless an error is thrown or the chain is intentionally terminated.

C. Resolved1 Resolved2 Resolved3 Resolved4 –
Incorrect. This option completely ignores the rejection. It would only be correct if the promise resolved instead of rejected. Since the promise rejects, the .catch() executes and the preceding .then() handlers are skipped.

Reference:

MDN Web Docs – Promise.prototype.catch() method

MDN Web Docs – Promise chaining and error handling

MDN Web Docs – Returning from catch and continuing chains

Salesforce Trailhead – JavaScript Essentials: Asynchronous Programming and Promises

That's the last free question in Asynchronous Programming

You've seen the free 30% (5 of 19 questions). Full access unlocks every question and full-length Salesforce-JavaScript-Developer mock exams (65 questions, 105 minutes), a different mock on every attempt.

Unlock full mock exam
Full mock exam

Sit the Salesforce-JavaScript-Developer exam before exam day

Full-length mocks with the same number of questions and the same time limit as the real Salesforce-JavaScript-Developer exam. Every attempt draws a new set of questions from the full bank, so you can keep going until you pass comfortably.

Get full Salesforce-JavaScript-Developer mock exam
  • 65questions per mock
  • 105 mintime limit
  • 147questions in the bank
  • Newmock on every attempt