PROMISES IN JAVASCRIPT
Introduction
• A Promise in JavaScript represents a value that may be
available now, later, or never.
• It is used to handle asynchronous operations efficiently.
Why Use Promises?
• Avoid callback hell (pyramid of doom)
• Better error handling
• Easier to read and maintain
States of a Promise
1. Pending – Initial state, the operation is not complete.
2. Fulfilled – Operation completed successfully.
3. Rejected – Operation failed.
Creating a Promise
let myPromise = new Promise((resolve,
reject) => {
let success = true;
if (success) {
resolve("Operation Successful");
} else {
reject("Operation Failed");
}
});
Handling a Promise
• Using .then() and .catch()
myPromise
.then(result =>
console.log(result)) // Success handling
.catch(error =>
console.log(error)); // Error handling
• Using async/await (Modern approach)
async function handlePromise() {
try {
let result = await myPromise;
Chaining Promises
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
Promise Methods
Promise.all()
• Waits for all promises to resolve or rejects if any fail.
Promise.all([promise1,
promise2]).then(results =>
console.log(results));
Promise.race()
• Returns the result of the first resolved or rejected promise.
Promise.race([promise1,
promise2]).then(result =>
Conclusion
• Promises make asynchronous programming more
manageable.
• Use async/await for better readability.
• Know Promise methods for handling multiple async tasks.
End of Presentation

Javascript topic on Promises slides made using Napkin

  • 1.
  • 2.
    Introduction • A Promisein JavaScript represents a value that may be available now, later, or never. • It is used to handle asynchronous operations efficiently.
  • 3.
    Why Use Promises? •Avoid callback hell (pyramid of doom) • Better error handling • Easier to read and maintain
  • 4.
    States of aPromise 1. Pending – Initial state, the operation is not complete. 2. Fulfilled – Operation completed successfully. 3. Rejected – Operation failed.
  • 5.
    Creating a Promise letmyPromise = new Promise((resolve, reject) => { let success = true; if (success) { resolve("Operation Successful"); } else { reject("Operation Failed"); } });
  • 6.
    Handling a Promise •Using .then() and .catch() myPromise .then(result => console.log(result)) // Success handling .catch(error => console.log(error)); // Error handling • Using async/await (Modern approach) async function handlePromise() { try { let result = await myPromise;
  • 7.
    Chaining Promises fetch('https://api.example.com/data') .then(response =>response.json()) .then(data => console.log(data)) .catch(error => console.log(error));
  • 8.
    Promise Methods Promise.all() • Waitsfor all promises to resolve or rejects if any fail. Promise.all([promise1, promise2]).then(results => console.log(results)); Promise.race() • Returns the result of the first resolved or rejected promise. Promise.race([promise1, promise2]).then(result =>
  • 9.
    Conclusion • Promises makeasynchronous programming more manageable. • Use async/await for better readability. • Know Promise methods for handling multiple async tasks.
  • 10.