What are JavaScript PROMISES? đ€
VloĆŸit
- Äas pĆidĂĄn 20. 12. 2023
- #JavaScript #tutorial #courses
// Promise = An Object that manages asynchronous operations.
// Wrap a Promise Object around {asynchronous code}
// "I promise to return a value"
// DO THESE CHORES IN ORDER
// 1. WALK THE DOG
// 2. CLEAN THE KITCHEN
// 3. TAKE OUT THE TRASH
// Promise = An Object that manages asynchronous operations.
// Wrap a Promise Object around {asynchronous code}
// "I promise to return a value"
// PENDING -> RESOLVED or REJECTED
// new Promise((resolve, reject) => {asynchronous code})
// DO THESE CHORES IN ORDER
// 1. WALK THE DOG
// 2. CLEAN THE KITCHEN
// 3. TAKE OUT THE TRASH
function walkDog(){
return new Promise((resolve, reject) => {
setTimeout(() => {
const dogWalked = false;
if(dogWalked){
resolve("You walk the dog đ");
}
else{
reject("You DIDN'T walk the dog");
}
}, 1500);
});
}
function cleanKitchen(){
return new Promise((resolve, reject) => {
setTimeout(() => {
const kitchenCleaned = true;
if(kitchenCleaned){
resolve("You clean the kitchen đ§č");
}
else{
reject("You DIDN'T clean the kitchen");
}
}, 2500);
});
}
function takeOutTrash(){
return new Promise((resolve, reject) => {
setTimeout(() => {
const trashTakenOut = true;
if(trashTakenOut){
resolve("You take out the trash â»");
}
else{
reject("You DIDN'T take out the trash");
}
}, 500);
});
}
walkDog().then(value => {console.log(value); return cleanKitchen()})
.then(value => {console.log(value); return takeOutTrash()})
.then(value => {console.log(value); console.log("You finished all the chores!")})
.catch(error => console.error(error));
Thank you bro!You made me understand promises!
Finally, I understand PROMISES. Thanks
Your video made promises clear in my head. Thank you for your effort.
It is the best explanation ever.
Thanks
Take out the trash "is really quick" => open the window/door - throw the trash - close. đ .... Thank you! Getting closer to understand. (The value in the end is a bit "confusing", but I will make some studies and samples.)
async/await simplifies the process in the next topic
This is super helpful, last night was having trouble wrapping my head around this but this video really made it click! Thanks man!
Bro, you rock. Thanks for the video!
The best tutorial on promises. Thanks mannn
Legend. Such a hard concept to grasp, and after watching your video and following along closely, I understand Promises now. Entertaining, imo funny, and educational video. You never disappoint me. Thank you so much. You're awesome!
Very good thank you.
Wow, after watching many videos finally i understood. Thanks.
I finally fully understood. Thanks man.
Thank you, it was very clear and simple.
The way of your explain is Awesome #BroCodez
Thank You
Wow thanks a lot. I was struggling to understand but now I do.
really helpful. Thanks
Man i love you sm i had such a hard time understanding callbacks and promises
i just searched about this tomorrow and you uploaded it today what are the odds ;)
Youre a savior man!
Thank you, it really helped me
THE BEST EXPLANATION
Thank you very much
You are awesome
The best!
Thank you
Yuuuhh, killed it mann
sharp.
Honestly I find the "pyramid of doom" less confusing than promises
đđđđ same
đ It gets easier with a lot of repetition⊠It was hard to wrap my head around but just going back over and over and coding along simultaneously can & will do the trick.
2.5 seconds to clean the kitchen? Damn, that's a long time!
where was this when i was banging my head against the wall learning this ;.;
I was probably still recording it lol
OO LA LA
Can you please explain the code you wrote inside the then() method. What does it do and why are we creating the arrow function with value parameter and how is it able to access the resolve value
When you pass the value into the resolve, that value parameter is what gets sent to the ".then()" function (as a parameter) written at the end of the file. Just how promises work
I noticed some tutorials will create a variable equally a new promise kinda like this "var p = new Promise((resolve, reject))" ... in your example you returned promises, is there a preferred way or this situational ?
So basiclly, we created async to make code run parallely,... then we found out that it caused some errors,... so we again create promises to make the async code, run sychronously đ
Can you not method chain instead of returning the values of the promises?
I understood it, but you made it a little unclear than it should have. Using .then.then.then without returning anything would've been a little clearer maybe, but there are a few other ways.
If the code is asynchronous why does the first reject prevent the other functions from being executed
Because it promises to return a value, but the next function depends on the value.
Hi, why do we need the return when calling another chore?
how he do this 1:50 ? This little picture ?
window key + > , for mac is CTRL + CMD + Space
â@@hongquannguyen5532 Thank you, very appreciate !
thanks bud
this is more complicated and verbose than callback hell đ