What are JavaScript PROMISES? 🤞

  Рет қаралды 90,829

Bro Code

Bro Code

Күн бұрын

#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

Пікірлер: 78
@BroCodez
@BroCodez Жыл бұрын
// 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));
@salimthedon
@salimthedon 6 ай бұрын
Thank you bro!You made me understand promises!
@iowatheother4986
@iowatheother4986 Ай бұрын
can you also show other ways to compare? like do you always need to use function? can you use other things? with every word, you should say what else can be used, like with the => also, what else could be used? etc.
@maialso6096
@maialso6096 5 ай бұрын
Here some questions that helped me understand the video more ! I'm absolute beginner so don't judge 2:40 1) Why do we need callbacks to do these chores in order? I was wondering why don't we just call the functions like this.. walkDog(); cleanKitchen(); takeOutTrash(); We can't do this because the output is going to be >> You take out the trash >> You walk the dog >> You clean the kitchen So the function that takes less time is going to be executed first. Since we want to make sure that each chore is going to be done after the another in order we have to use callbacks since the next function It will never be called unless the function fully executes 3:10 2) What is Callback hell? is an old pattern to handle asynchronous functions I watched the video Callback hell , and It also answers the first question 3) Why do we need a new arrow function to act as it our callback and not use the functions that we already have ? walkDog(cleanKitchen(takeOutTrash)) I tried doing something like this , with the last function without a parameter since there is no next chore..It returned an error that the passed argument to walkDog is not a function ..and I checked the type by console.log(typeof cleanKitchen(takeOutTrash)) and it was undefined ..So this method only works for two chores maximum like this.. --> cleanKitchen(takeOutTrash) and the output will be following the order correctly 6:33 4) How can the function provide a value parameter ? one snippet of code that made me understand better is let prom = walkDog() // returns a promise object and store in prom setTimeout(() => {console.log(prom)}, 3000) /*which is going to make sure that the result of the promise is not pending , logging it without the timeout will output Promise { } which is also cool */ the output is going to be >> Promise { 'You walk the dog' } so we are just like passing the value to then handler like the following .. Promise {value}.then( value=>console.log(value)) ( searching different sites mainly FreeCodeCamp) our function walkDog is returning a promise object which can have there different results .. undefined: Initially when the state value is pending. value: When resolve(value) is called. error: When reject(error) is called. The .then() method should be called on the promise object to handle the value or the error of the promise. the parameters of then receive data from the Promise then(successFunc, rejectFunc) returns a promise If you are interested only in successful outcomes, you can just pass one argument to it. 7:22 5) Why returning cleanKitchen and not just calling it ? Because we want to return what the function returns so simply .. return what cleanKitchen returns cleanKitchen returns a promise and we want to returns that promise It just like storing the returned promise to const prom and then returning it like.. const prom = cleanKitchen() return prom we need a promise since we are going to attach the handler then to it
@TakiBARKAT-eh8rw
@TakiBARKAT-eh8rw 5 ай бұрын
thank you bro, you really helped me
@divyaanshuagrawal3383
@divyaanshuagrawal3383 5 ай бұрын
Bro i was having the same doubt as your 3rd point! Thanks for sharing!
@JW-uc6md
@JW-uc6md 5 ай бұрын
thanks !
@hamodi20091
@hamodi20091 Жыл бұрын
Finally, I understand PROMISES. Thanks
@jmg9509
@jmg9509 Ай бұрын
must be nice
@anonymousguy6614
@anonymousguy6614 7 ай бұрын
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!
@SaintHanappi
@SaintHanappi Жыл бұрын
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.)
@BroCodez
@BroCodez Жыл бұрын
async/await simplifies the process in the next topic
@gokhanozdemir8970
@gokhanozdemir8970 10 ай бұрын
Your video made promises clear in my head. Thank you for your effort.
@altlalit
@altlalit Жыл бұрын
It is the best explanation ever. Thanks
@Ethan-Breitkreutz
@Ethan-Breitkreutz 10 ай бұрын
This is super helpful, last night was having trouble wrapping my head around this but this video really made it click! Thanks man!
@Elizabeth_Calmau
@Elizabeth_Calmau 2 ай бұрын
Why didn't I saw this video before? Now that's so clear! Thank you 🤗
@haidermansoor4760
@haidermansoor4760 11 ай бұрын
The best tutorial on promises. Thanks mannn
@rbsfinger
@rbsfinger 11 ай бұрын
Bro, you rock. Thanks for the video!
@Granta_Omega
@Granta_Omega 6 ай бұрын
2.5 seconds to clean the kitchen? Damn, that's a long time!
@Pururin_Purin
@Pururin_Purin Жыл бұрын
Honestly I find the "pyramid of doom" less confusing than promises
@pidli
@pidli 11 ай бұрын
😂😂😂😂 same
@jeremyfrias7
@jeremyfrias7 10 ай бұрын
😅 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.
@jktrivedi29
@jktrivedi29 7 ай бұрын
Wow, after watching many videos finally i understood. Thanks.
@engenheirodesoftware
@engenheirodesoftware Жыл бұрын
Very good thank you.
@mohantraju
@mohantraju 10 ай бұрын
really helpful. Thanks
@sauzxa
@sauzxa 5 ай бұрын
what a simple explanation after like 10 videos for that thx you bro 🙏
@SuperDude101
@SuperDude101 Жыл бұрын
i just searched about this tomorrow and you uploaded it today what are the odds ;)
@itzvaibhavkumar3029
@itzvaibhavkumar3029 7 ай бұрын
Man i love you sm i had such a hard time understanding callbacks and promises
@kianaparsi8223
@kianaparsi8223 2 ай бұрын
Fantastic, finally i got it🙏🙏❤️❤️
@mintymintfresh
@mintymintfresh 4 ай бұрын
awsome explanation !!
@lillianirungu8180
@lillianirungu8180 8 ай бұрын
Wow thanks a lot. I was struggling to understand but now I do.
@mzedan2
@mzedan2 10 ай бұрын
Thank you very much
@hilmimusyafa
@hilmimusyafa 5 ай бұрын
BRO FINALLY I UNDERSTAND AFTER 5 VIDEOS AND COURSES
@s21ekosn8
@s21ekosn8 4 ай бұрын
thanks
@lambo-ca
@lambo-ca 9 ай бұрын
I finally fully understood. Thanks man.
@open2003
@open2003 9 ай бұрын
You are awesome
@yy.u.i
@yy.u.i 9 ай бұрын
Thank you, it was very clear and simple.
@renatocorreia1805
@renatocorreia1805 Жыл бұрын
THE BEST EXPLANATION
@ShaileshKumar-re6yz
@ShaileshKumar-re6yz 8 ай бұрын
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
@drewlee7435
@drewlee7435 6 ай бұрын
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
@mr.saiprasad5840
@mr.saiprasad5840 Жыл бұрын
The way of your explain is Awesome #BroCodez Thank You
@rajatsachann
@rajatsachann 9 ай бұрын
Youre a savior man!
@bordercut1
@bordercut1 7 ай бұрын
sharp.
@sekwayimokoena3225
@sekwayimokoena3225 Жыл бұрын
The best!
@oshadhaedirisinghe1455
@oshadhaedirisinghe1455 7 ай бұрын
Thank you
@J4nlks
@J4nlks 8 ай бұрын
Yuuuhh, killed it mann
@aaronamoso
@aaronamoso Ай бұрын
running thru this like Usane Bolt before my full-stack dev intern interview
@saimadhav945
@saimadhav945 15 күн бұрын
how did the interview go
@aaronamoso
@aaronamoso 15 күн бұрын
@@saimadhav945i was ghosted ):
@ОлександрКанюка-ч2ъ
@ОлександрКанюка-ч2ъ 9 ай бұрын
Thank you, it really helped me
@alexidino
@alexidino 10 ай бұрын
how he do this 1:50 ? This little picture ?
@hongquannguyen5532
@hongquannguyen5532 9 ай бұрын
window key + > , for mac is CTRL + CMD + Space
@alexidino
@alexidino 9 ай бұрын
​@@hongquannguyen5532 Thank you, very appreciate !
@abhishekvashistha4017
@abhishekvashistha4017 9 ай бұрын
thanks bud
@andrewchukwudumeje9413
@andrewchukwudumeje9413 8 ай бұрын
If the code is asynchronous why does the first reject prevent the other functions from being executed
@chomantsang3906
@chomantsang3906 6 ай бұрын
Because it promises to return a value, but the next function depends on the value.
@Ershaad-ss3uc
@Ershaad-ss3uc 7 ай бұрын
OO LA LA
@DamosyTheFreckle
@DamosyTheFreckle 6 ай бұрын
Hi, why do we need the return when calling another chore?
@anonymous13378
@anonymous13378 Ай бұрын
someone plz tell is this video enough to understand promise to do i need to dive in deeper through docs and other longer videos
@SemiatBalogun
@SemiatBalogun 4 күн бұрын
Anyone here can help me in the comment section. From what I understand, isn’t the task with the shortest time supposed to run first. I think the whole point of asynchronous operation is that it is not carried out in an orderly manner like that. It doesn’t wait until one resolves/reject before carrying out other tasks or running other codes. I think it simply waits for the pending task while running other codes or performing other tasks. This is what I’ve been able to gather from everything I’ve been reading online but this video says kind of opposite, like they wait for each other. Someone should please hlep. Or is this tutorial on synchronous operations
@Isaac-ro1jl
@Isaac-ro1jl 2 ай бұрын
Using the ? operator makes simple stuff much easier, check it out : function task1() { return new Promise((resolve, reject) => { let completed = false; setTimeout(function () { completed ? resolve("task1 completed") : reject("didint complete task1"); }, 3000); }); }
@beepboopitsjoop4678
@beepboopitsjoop4678 Жыл бұрын
where was this when i was banging my head against the wall learning this ;.;
@BroCodez
@BroCodez Жыл бұрын
I was probably still recording it lol
@user-cup_-nu4kg
@user-cup_-nu4kg Жыл бұрын
Can you not method chain instead of returning the values of the promises?
@amitpaul2361
@amitpaul2361 3 ай бұрын
What if multiple rejects come??
@sedhunique
@sedhunique 6 ай бұрын
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 💀
@TragicGFuel
@TragicGFuel 5 ай бұрын
No, it seems you have no fundamental understanding of how javascript runs. It's not parallel.
@bryanquartey-papafio7628
@bryanquartey-papafio7628 2 ай бұрын
​@@TragicGFuel he was right at the last part tho it did make it run synchronously
@TragicGFuel
@TragicGFuel 2 ай бұрын
@@bryanquartey-papafio7628 no he's misunderstood event based async with truly parallel async. I doubt he would know the difference between concurrent and parallel
@bryanquartey-papafio7628
@bryanquartey-papafio7628 2 ай бұрын
@@TragicGFuel oh wait I see
@rustyking23able
@rustyking23able 11 ай бұрын
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 ?
@xXAngelmlXx
@xXAngelmlXx 8 ай бұрын
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.
@Chae_boll
@Chae_boll 4 ай бұрын
31/8/2024 day 1 : 12:36 ✅✅
@Matheus-mr4tl
@Matheus-mr4tl 6 ай бұрын
this is more complicated and verbose than callback hell 😅
@deltha2838
@deltha2838 Ай бұрын
callback hell 10x better
@Chae-dudu
@Chae-dudu 4 ай бұрын
31/8/2024 day 1: 12:36
JavaScript ASYNC/AWAIT is easy! ⏳
4:39
Bro Code
Рет қаралды 118 М.
How to FETCH data from an API using JavaScript ↩️
14:17
Bro Code
Рет қаралды 181 М.
Jaidarman TOP / Жоғары лига-2023 / Жекпе-жек 1-ТУР / 1-топ
1:30:54
JavaScript Promises Crash Course
24:03
Kevin Powell
Рет қаралды 40 М.
JS Promises in 4 Minutes
3:51
CodeNexus
Рет қаралды 2,4 М.
Learn JSON files in 10 minutes! 📄
10:09
Bro Code
Рет қаралды 71 М.
JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue
12:35
8 Rules For Learning to Code in 2025...and should you?
12:59
Travis Media
Рет қаралды 247 М.
Async JavaScript & Callback Functions -- Tutorial for Beginners
24:21