Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


When using await, what must you ensure about the function it's used in?

  1. It must be marked as async.

  2. It must return a Promise.

  3. It must be a generator function.

  4. It must not have parameters.

The correct answer is: It must be marked as async.

When using `await`, it is essential to ensure that the function in which it is used is marked as async. This is because `await` can only be applied within an async function. The purpose of the async keyword is to enable asynchronous operations in a more readable manner, allowing for the use of `await` to pause execution until a Promise is resolved or rejected. The significance of marking a function as async is that it implicitly makes the function return a Promise. This means that using `await` allows for cleaner syntax without needing to chain `.then()` methods, which can lead to more complex and harder-to-read code when dealing with multiple asynchronous operations. Using `await` inside non-async functions would lead to a syntax error, highlighting the importance of marking the function correctly to utilize the benefits of async programming. While other options may introduce aspects related to Promises or function types, they do not address the fundamental requirement for using `await` effectively within code.