JavaScript ASYNC/AWAIT is easy! ⏳

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

const cleanKitchenResult = await cleanKitchen();

const takeOutTrashResult = await takeOutTrash();

}
catch(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();


const takeOutTrashResult = await takeOutTrash();


console.log("You finsihed all the chores!");
}
catch(error){
console.error(error);
}
}

doChores();

BroCodez
Автор

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!!

TheComputerSciencestudent.
Автор

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

martinfriday
Автор

i really struggled with understanding async and await but you explained it too good. Thank

Abood-wnfi
Автор

short but perfect and easy to understand.
Thanks for sharing good tips.

nysmulerecords
Автор

Bro are you a full time youtuber or working for a company?, your coding skills are really good

footy
Автор

Thank you for this! This was really clear to understand. I think seeing the Promise function and the resolve and reject parameter helped

darrenharris
Автор

Thanks bro explained it so love you brother .❤

iftyrahman
Автор

short and crisp video thnx for sharing

x..darkfate..x
Автор

This is much better than the last .then video and useful.

robeeeeen
Автор

Thankyou sir your teaching style is awesome❤

yuvarajsingh
Автор

Seems like an error catching and resolving way of coding realistically giving you ways to output custom errors pretty cool actually thanks for this.

Smurfis
Автор

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

reggiehurley
Автор

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 <button> 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

alexaldridge
Автор

i started learning node.js and im explding its too hard :((

hunin
Автор

which tutorial playlist I should follow, this 2024 one or the older one?

Taofetch
Автор

Is there a way to get the data from inside the async function saved to a variable for later use?

antcannon