I think I'm confident now using Promises as well as async/await any where thanks to you. I mostly needed to know about error handling as I didn't care for it before. 🤦♂ In the last exercise I think using a Promise is actually more readable.
That’s exactly what I love to hear! It’s a tricky concept so feeling confident with all its uses it’s huge 😊 And yeah, it’s very subjective especially with “smaller” examples like this which method people prefer. Sometimes a team just goes with one method as a standard that they all agree on too.
sir to your 1st exercise one solution could also be this :- const fetchUser=()=>{ return new Promise((resolve,reject)=>{ setTimeout(()=>{ resolve({data:{user:"Monkey",admin:true}}); },3000) }) } console.log('Program Starting'); const login=(obj)=>{ if("admin" in obj){ if(obj.admin===true){ console.log('Successfully logged in'); }else{ console.log('Failed to login in, please try again'); } }else{ console.log("Failed to login in ,please try again"); } } const func=async()=>{ const res=await fetchUser(); login(res); console.log('Program complete'); } func();
Hey Abhishek! Nice work on this! It's really showing that you're understanding this concept more inside and out, awesome to see! I like yours better since you are actually waiting for things in the correct order like the "Program complete". Well done 🤓
Hey again Abhishek! Great work! It's so important to get practice with turning the vanilla promise chains in to async/await and vice versa. Hopefully you agree that it really increases the understanding 🙂
Great job Nader, this series has been so helpful. I was blazing through the code as I was writing it, although I'm sure I'll forget it all tomorrow lol
Thanks for this excellent video. However there is an error in exercise 1. Promise should resolve to {user: "Monkey", admin: true} and not { data: {user: "Monkey", admin: true} } To test, give ChatGPT the exercise and then run the results it outputs Note that your instructions tell reader to do login(result); while what will actually give successful response is login(result.data);
Hey Trae, ah I see what you mean. I think the instructions could be a bit more clear as they state to "pass the user" to the "login" function. I guess this can be interpreted in a couple ways as passing the whole "result/response" object or just the "user/data" inside. Nice catch and love that you test it with ChatGPT, haha! 😃
The comments directive are confusing! 16:31 You asked if the object has a property called 'admin' ect.... So you asked if there is a property with the name 'admin'. Why didn't you do like this: if(user.hasOwnProperty('admin') && (user.admin === true)) The comments directives are not followed thoroughly
Hey Aata, apologies for them being confusing - they were tricky to write succinctly on one screen 😄 You’re right, technically we should check for hasOwnProperty, however since we haven’t learned prototypes or prototype chains yet I’ve been leaving it out since I wanted to focus on the async concepts for this video. Love your check for the property though as it’s way more robust than mine! 🤓
Async-Awaits a blessing, on exercise 3 I forgot to set the function goGetCandies to a variable and got stuck for the longest lol. I did it super fast with the async-await function, but with vanilla promises it gets complicated super fast when your promises are wrapped in functions.
Hi Nader, thank you for the video again! Sort of unrelated but can you tell my why you prefer function expressions vs function declaration? Is it no beneficial to hoist functions or does it save memory or something else?
Hey Calvin! It's mainly because they are so common within callbacks and anonymous expressions, like within map/filter/reduce and stuff like that. Also, when you get to how the "this" keyword works, if we don't need to use "this" then we can also use arrow functions instead of "regular" functions. Generally, we shouldn't really rely on hoisting in general so I don't really consider that a plus haha 😊 That said... there are times where we absolutely do need non-arrow functions, so it's good to know both - this will be more apparent when we see the "this" context as I mentioned 😉
I'm having a bit of difficulty in understanding why the code below is logging Promise { } and then {candy: 'sour keys', quantity: 10 } In vanilla promise part of exercise-03, instead of using the resolved value from the goGetCandies function directly into the sellCandies function, I first saved the resolvedValue in a variable. When I log that variable, it logs Promise { } and then { candi: 'sour keys', quantity: 10 }. And then when I pass on this variable to the sellCandies function, its giving NaN. Could you please explain why I can't save the resolved value in a variable and when passed into the sellCandies function its not outputting 250 but NaN. I hope I havent confused you with my question!
That’s really interesting! Could I ask you to post what your code looks like to get that result? It should always give you the resolved value in the .then so something weird must be happening. My guess is that NaN comes from trying to do math with a promise or undefined instead of a number 😄
For exercise 2 did you forget the last step 8? It ask how we could make it run faster, i believe i done this with Promise.all() const fetchBothFaster = async () => { const both = await Promise.all([fetchFast(), fetchSlow()]); console.log("#2 Both",both[0]); console.log("#2 Both",both[1]); console.log("#2 Time =",(new Date() - firstTime)); //around 6005ms, shaving off 2 seconds of fetchFast() } Would this be the correct solution for making it run faster?