Async & Promiseseasy
What is a Promise in JavaScript and how is it used?
Describe what a Promise is in JavaScript, its states, and how it is utilized to handle asynchronous operations.
Answer
A Promise is an object representing the eventual completion (or failure) of an asynchronous operation.
States:
- Pending: Initial state, neither fulfilled nor rejected.
- Fulfilled: The operation completed successfully.
- Rejected: The operation failed.
Usage:
- Use the 'then()' method to handle fulfilled promises.
- Use 'catch()' to handle rejected promises.
- Chain multiple promises for sequential asynchronous tasks.