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.


Which Promise method correctly handles both resolve and reject?

  1. Promise.then()

  2. Promise.catch()

  3. Promise.finally()

  4. Promise.all()

The correct answer is: Promise.then()

The Promise method that correctly handles both resolve and reject is represented by the first choice, which uses the `then()` method. The `then()` method takes two arguments: the first is a callback function that is executed when the Promise is resolved (successful completion), and the second is a callback function that is executed when the Promise is rejected (an error occurs). This dual capability allows `then()` to manage resolution and rejection, making it a powerful tool for asynchronous programming. While the other methods serve important roles in working with Promises, they do not handle both outcomes in the same way. The `catch()` method specifically handles only the rejection of a Promise, allowing you to specify what happens when an error occurs. The `finally()` method allows you to execute code after a Promise settles (whether it resolves or rejects), but it does not provide a way to handle those outcomes separately. Lastly, `Promise.all()` is used to execute multiple Promises in parallel and is concerned with resolving when all provided Promises have resolved, but it does not directly manage individual resolve or reject cases. The nature of `then()` in handling both scenarios makes it indispensably effective in Promise chaining and error handling.