JavaScript ASYNC/AWAIT is easy! ⏳
Вставка
- Опубліковано 20 жов 2024
- #javascript #tutorial #programming
// Async/Await = Async = makes a function return a promise
// Await = makes an async function wait for a promise
// Allows you write asynchronous code in a synchronous manner
// Async doesn't have resolve or reject parameters
// Everything after Await is placed in an event queue
async function doChores(){
try{
const walkDogResult = await walkDog();
console.log(walkDogResult);
const cleanKitchenResult = await cleanKitchen();
console.log(cleanKitchenResult);
const takeOutTrashResult = await takeOutTrash();
console.log(takeOutTrashResult);
console.log("You finsihed all the chores!");
}
catch(error){
console.error(error);
}
}
doChores();
// Async/Await = Async = makes a function return a promise
// Await = makes an async function wait for a promise
// Allows you write asynchronous code in a synchronous manner
// Async doesn't have resolve or reject set up as parameters
// Everything after Await is placed in an event queue
function walkDog(){
return new Promise((resolve, reject) => {
setTimeout(() => {
const dogWalked = true;
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);
});
}
async function doChores(){
try{
const walkDogResult = await walkDog();
console.log(walkDogResult);
const cleanKitchenResult = await cleanKitchen();
console.log(cleanKitchenResult);
const takeOutTrashResult = await takeOutTrash();
console.log(takeOutTrashResult);
console.log("You finsihed all the chores!");
}
catch(error){
console.error(error);
}
}
doChores();
walkDog()
.catch(error => {console.error(error); return error; })
.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!")})
// index.js:52 You DIDN'T walk the dog 🦮
// index.js:53 You clean the kitchen 👨🏻🍳
// index.js:54 You take out the trash 🚮
// index.js:54 You finished all the chores!
when if the a promise rejected still I can resolve rest of the promises using promise chaining
but for async/await it will not happen? how I can make it happened in the same way?
I think you could have removed the explicit Promises in those chore functions and marked them as async then used the return/throw syntax to mimic the resolve/reject syntax since in practice we don't really create new Promsie objects. What's what the async function is for!
so good to see you still uploading thanks for the C# videos I am a freshmen in College as CS major and I learnt a a lot from your videos. Just wanted to say THANKS BRO!!
I have been squabbling with this for a loooonnng time, this tutorial was super helpful and very straight to the point, thank you soo much
You are one of the best teachers on UA-cam. I understand so well listening to your lectures. It even feels satisfying and calming to just learn from you 😊.😂🤪
i really struggled with understanding async and await but you explained it too good. Thank youuuuu
Thank you for this! This was really clear to understand. I think seeing the Promise function and the resolve and reject parameter helped
short but perfect and easy to understand.
Thanks for sharing good tips.
THE best JS instructionals
Seems like an error catching and resolving way of coding realistically giving you ways to output custom errors pretty cool actually thanks for this.
Thanks bro thanks.....You explained it so easily.....I love you brother .❤
Bro are you a full time youtuber or working for a company?,your coding skills are really good
you made it easy! thanks for this!
So clear! thank you!
bro correct me if i'm wrong but in the first ex that results in the "resolve is not defined" error that has nothing to do with async vs not async function
Thankyou sir your teaching style is awesome❤
Hey Bro Code great video. Once I have gotten concepts like this down what do I search up to learn how to attach like the true or false area so it can be changed with lets say an html on a website. or how do I make it so the console logs so a user off the app can see it in nice text? (I feel Javascript is in the background how can I make it so a user can interact with this in an app?) Cheers
short and crisp video thnx for sharing
when you said heres what happens when you run this code, i felt like a monster was going to jump out when it came back with an error lol.
This is much better than the last .then video and useful.
Very helpful. Thanks for the video.
you are brilliant man
Very much needed!❤
really helpful,Thank You sir
which tutorial playlist I should follow, this 2024 one or the older one?
I`m one hour passed in the full 8 course vid and stuck between choosing what to follow now
This playlist is the updated version
@@BroCodezthankyou and have a great day bro!
@@BroCodez can you do a new video with more advanced c++ concepts pls. like polymorphism, encapsulation, abstraction, etc. and also like vectors, the auto keyword, and concepts that are new to the current version of c++ please my fellow bro
i started learning node.js and im explding its too hard :((
Go programming language has made by life much easier
pls dont explde
ill try. i start to learn a bit more@@NAMEUA-camR
so did you learned it?
@@ALLSTARDECOURO2 yes
Is there a way to get the data from inside the async function saved to a variable for later use?
Thank you very much
Can you do it in c#?
Thank you
Make a in depth asynchronous video
Is Next Js Is the future?
so what u think ?
Lov u bro
good stuff
catch (error) {
throw(error);
}
901 Antwan Squares
132 Howell Shoals
FUCKING THANK YOU !!!!!
441 Kohler Wall
Geovanni Road
31/8/2024
day 1: done ✅✅
Helo
36633 Rosario Fall