What are JavaScript PROMISES? 🤞

preview_player
Показать описание
#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));

BroCodez
Автор

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 ?

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 { <pending> } 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

maialso
Автор

Finally, I understand PROMISES. Thanks

hamodi
Автор

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!

anonymousguy
Автор

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.)

SaintHanappi
Автор

It is the best explanation ever.
Thanks

altlalit
Автор

Your video made promises clear in my head. Thank you for your effort.

gokhanozdemir
Автор

what a simple explanation after like 10 videos for that thx you bro 🙏

sauzxa
Автор

Honestly I find the "pyramid of doom" less confusing than promises

Pururin_Purin
Автор

BRO FINALLY I UNDERSTAND AFTER 5 VIDEOS AND COURSES

hilmimusyafa
Автор

This is super helpful, last night was having trouble wrapping my head around this but this video really made it click! Thanks man!

Eteen
Автор

Wow, after watching many videos finally i understood. Thanks.

jktrivedi
Автор

2.5 seconds to clean the kitchen? Damn, that's a long time!

Granta_Omega
Автор

Wow thanks a lot. I was struggling to understand but now I do.

lillianirungu
Автор

Man i love you sm i had such a hard time understanding callbacks and promises

itzvaibhavkumar
Автор

i just searched about this tomorrow and you uploaded it today what are the odds ;)

SuperDude
Автор

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

ShaileshKumar-reyz
Автор

The best tutorial on promises. Thanks mannn

haidermansoor
Автор

I finally fully understood. Thanks man.

lambo-ca
Автор

Thank you, it was very clear and simple.

yy.u.i