Creating a Promise, Chaining & Error Handling | Ep 03 Season 02 Namaste JavaScript

preview_player
Показать описание
Promise Constructor is used with the new keyword to create a new Promise. In this video, we dive deeper into creating Promises and cover everything about Promise Chaining and Error Handling.

00:00 - Introduction
01:05 - Consuming Promises
02:56 - Creating a Promise
15:23 - Promise Rejection
16:21 - Error Handling
18:17 - Promise Chaining
22:33 - Advanced Promise Chaining
26:28 - Advanced Error Handling
32:24 - Recap of the video
35:35 - Teaser of the Next Video
36:10 - Homework for this episode
38:25 - Thank you for watching Namaste JavaScript 🙏

If you find this video helpful,
Please support me by giving a Super Thanks contribution to the video ♥️

If not by money,
you can also support this video series, by sharing it with your friends on Social Media. 🙏

I'll give my best to come up with great content and everything absolutely for free on YouTube. 😊

Also, give it a thumbs up and subscribe to my channel for more such videos. 🔔

Cheers,
Akshay Saini

Stay Connected:

#NamasteJavaScript #AkshaySaini
Рекомендации по теме
Комментарии
Автор

This Episode is a little long, but trust me everything taught is super important. Watch this video with full attention and also finish the homework given at the end of this video. All the Best. Keep Rising! 🚀

akshaymarch
Автор

If you are really interested to learn or know something really important, then you won't bother about the length of the video. No trash, No Bakwaas, just pure concepts and real-time things. I loved it. Big hug Akshay 🤗.

abhishekkadavergu
Автор

Most beautiful playlists on JavaScript.
Binge watched the 23+4=27 videos in 3 days.
*Please continue this series.*

DebojyotiMandal
Автор

when can we expect EP-04 ???
you made me fall in love with JavaScript, Amazing content

spillthebuzz
Автор

1. Promise can be created using a new Promise() constructor function.
2. This constructor function takes a callback function as argument.
3. The callback function has 2 arguments named 'resolve' and 'reject'. Resolve and reject are the keywords provided by JS.
4. We can only resolve or reject a promise. Nothing else can be done.
5. An error can also be created using new Error('error message').
6. There is also .catch() which is used to attach a failure callback function that handles any error that pops up during the execution of promise chain.
7. .catch only handles error of .then() that are present above it. If there is any .then() below it, catch will not handle any error for that, also that, then will get executed no matter what.
8. It can be useful in a way if we want to catch error for a particular portion of a chain.
9. We can have multiple catch based on requirement and then a general catch at the end.
10. Always remember to return a value in the promise chain for the next .then to use .
11. If it returns a value => It will be used as an argument in next function. If it is a promise then the next .then in the promise chain is attached to the promise returned by the current callback function.

Homework:


const cart = ['shoes', 'pants', 'kurta'];

createOrder(cart)
.then(function(orderId) {
console.log(orderId);
return orderId;
})
.then(function(orderID) {
return proceedToPayment(orderID)
})
.then(function({ message, amt }) {
console.log(message, 'of amount:', amt);
return showOrderSummary(message, amt);
})
.then(function({ message, amt }) {
console.log('Your wallet has beed debited by:', amt);
})
.catch(function(err) {
console.log(err.message);
})
.then(function() {
console.log('No matter what happens, I will get executed');
});



function createOrder(cart) {
const pr = new Promise(function(resolve, reject) {
// create order
// Validate Cart
// orderId
if (!validateCart(cart)) {
const err = new Error('Cart is not valid!');
reject(err);
}
// logic for createOrder
const orderId = '12345';
if (orderId) {
setTimeout(function() {
resolve(orderId);
}, 5000)
}
});

return pr;
}

function proceedToPayment(orderID) {
// Logic for handling payment.
// This function returns a promise
return new Promise(function(resolve, reject) {
// logic
resolve({ message: `Payment Successful for order id: ${orderID}`, amt: 2500 });
})
}

function showOrderSummary(paymentInfo, amt) {
return new Promise(function(resolve, reject) {
// console.log(amt);
if (amt >= 2000) {
resolve({ message: 'You have ordered items that cost ${amt} RS', amt });
} else {
reject(new Error('Please buy more for discount'));
}
})
}

function validateCart(cart) {
// code to validate cart.
return true;
// return false;
}

jagrutsharma
Автор

Bhai...! Kya hi bolu main aapko! Just take my 🙏Namaskar🙏
You don't know how many problems of developers you've solved just by this video.

ALL_RUNDER
Автор

Big Thanks to Akshay sir
Pls bring the EP04, waiting for it.

rjsk
Автор

Bing watched both seasons in 2 days.. nd a big big thanks from bottom of my heart.. thanku sir ✨️

SimarjeetKaur-hx
Автор

Akshay you are great...you made me fall in love with JavaScript 😂... keep teaching us...lots of respect for you

sanaumar
Автор

Thanks Akshay for distilling JavaScript into your super easy videos. I just loved it. You're giving your precious time to teach all of us here, Kudos to you. Looking forward to your next awesome video.

manoharsingh
Автор

I had an interview a couple of days back. The interviewer asked me how we can continue executing few "then" blocks even if we have an error from one of them. To be honest, I didn't understand his question then. Now I know what he meant and have the answer after watching this video. Thanks so much Akshay !

keshavsharma
Автор

Akshay just want to say a big thank you. i have been working for over 10 years in IT industry but the clarity of javascript given by you for core concepts is invaluable. Not only it helped me in interviews but also helped to be a better developer. Amazing work!!!
I hope you'll amaze us every time and keep the good content coming :)

nishankarora
Автор

Thanks a lot for all your awesome videos. I know and respect each and every efforts behind each video !

ganeshkhirwadkar
Автор

Nowadays everyone is creating for beginners or a so called crash course but.... As developer I want to see such kind of videos where you will see deep scenario and realtime code debugging we like this length of videos Kudos to you Akshay bhaiya❤️

deadlock_
Автор

I binge-watched your JS series; Thank you for all the "Aha!" Moments. You are Pushpa and JS is Red-sanders; Thagede Le!

sdsgfhgthjj
Автор

It's here😍. For the first time in my life, I'm excited that a video is long, because I know I'll definitely get more quality content.

Anand
Автор

Now onwards it's super easy to keep promises in mind.😅
Thanks for this amazing content.😇

pratyushkumar
Автор

I fall in love with promises just because of you thank you so much you are i was promiseFobia but just because of you it thank you so much agai for making me jorney easy.... you are the i always sleepy when i am doing lecture but just because of you I just addicted to javascript like web after getting job i will definitely contrubute thank you so

poojalahane
Автор

Your playlist helped me a lot in my career.
Thanks Akshay

pavanyerra
Автор

00:00 🚀 Promises simplify asynchronous operations in JavaScript, enhancing code readability and developer productivity.
00:27 💼 The episode covers creating custom promises, error handling, and promise chaining with practical examples.
01:12 🛒 Example scenario: Creating an order in an e-commerce website using asynchronous operations.
02:03 🔗 Creating a promise involves using the Promise constructor with resolve and reject functions.
03:12 📝 Promises allow developers to control when asynchronous operations succeed (resolve) or fail (reject).
08:31 🔍 Gracefully handle promise rejections using the catch method to avoid silent errors in production.
15:10 🔄 Promise chaining enables sequential execution of asynchronous tasks, passing data between them for streamlined workflows.
20:13 🎯 Promise chaining allows passing data between asynchronous operations, simplifying complex workflows.
20:55 🔄 Promises are crucial for handling asynchronous operations, such as handling payments in an e-commerce scenario.
23:23 ❌ A common error in promise chaining is forgetting to return values down the chain, leading to unexpected behaviour and difficult debugging.
24:31 🔗 To pass data down the promise chain, always return the necessary values or promises from each link in the chain.
26:33 🛑 The catch method is essential for handling errors in promise chains, ensuring graceful error handling throughout the chain.
28:27 🔄 Placing a catch statement strategically allows specific error handling for different parts of the promise chain, ensuring robust error management.
30:01 🔄 Regardless of errors, the 'then' method following a 'catch' statement will always be executed, providing a reliable continuation point in the chain.
32:30 📚 A recap of key concepts covered: creating promises, error handling with catch, and chaining promises for sequential asynchronous operations.
36:14 📝 Homework assignment: Create a promise chain simulating an e-commerce scenario with four async operations, emphasizing proper chaining and error handling.

harishkanuka