Free Salesforce JavaScript Developer Practice Test Questions
Topic: Asynchronous Programming
147 questionsUpdated 23-Nov-20267 topics
TopicAsynchronous Programming
Question 1Choose 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?
Correct answer:
A. console.assert(await functionUnderTest(true), ' OK ' )
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 2Choose 2
Refer to the code:
01 const exec = (item, delay) = >
02 new Promise(resolve = > setTimeout(() = > resolve(item), delay));
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()?
Correct answer:
B. runParallel().then(function(data){
return data;
});
D. runParallel().then(data);
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 3Choose 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?
Correct answer:
D. 2 3 1
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 4Choose 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?
Correct answer:
B. setInterval(isDeg, 60000, ' cat ' );
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
That's the last free question in Asynchronous Programming
You've seen the free 25% of this topic (4 of 19 questions). Get complete access for every question, explanation and the timed exam simulator.
Our timed 2026 Salesforce JavaScript Developer practice test mirrors the format, number of questions and time limit of the official exam.
The #1 challenge isn't just knowing the material, it's managing the clock. The simulation builds your speed and stamina, so you walk in knowing exactly what to expect.