codedamn

Nesting Promises

Created by Codedamn about a year ago

0

No description provided

13 Comments

    0

    Lectures are so tiresome ;-(

    @adityavishwak

    Rambo Vladmir

    @adityavishwak

    0

    JavaScript syntaxes are confusing

    @ashrrithk

    Ashrrith Karunaakar

    @ashrrithk

    1

    The only thing clear to me is that the speaker knows his js promises. Apart from that, not much else is.

    @jorghydel

    Arijit Gogoi

    @jorghydel

    2

    I couldn't get anything from this video please make a clear explanation

    @coderpatel

    Mithlesh Kumar

    @coderpatel

    3

    Being an intermediary I get what you are trying to explain but it's definitely not a very helpful video for beginners.

    @s47mson

    Samson Shukla

    @s47mson

    1

    Console bottom is not properly visible. Same issue happening for multiple videos.

    @ramnathr

    Ramnath Repakula

    @ramnathr

    2

    for a beginner the .then .catch and promise is too difficult

    @hs7738

    Hitesh Pal

    @hs7738

    2

    Bro i did not pay you 200 Euro for this bad explation !!! i did not get anything!!

    @karo21

    Karwan Lazgeen

    @karo21

    4

    it's so hard to understand these content like everything got jumbled

    @sudhak63cb7083

    sudha kumari

    @sudhak63cb7083

    3

    Your explanation is super horrible here, Please replace this video with nice explanation. Did not expect this after paying money

    @nit061

    NITHIN S

    @nit061

    4

    for all users who don't understands the concept: Promises are like objects in javascript but async in nature. Javascript is a synchronus language, it means it starts from the top and execute everything till code ends. So if any async code is written it will not execute the code below it because async code might take some time like fetching data from API. To prevent this javascript introduced callback function which was way to hard then they introduced promises. So when we need to fetch data we can use "fetch" which returns a PROMISE not an OBJECT . Although promises and object are alike but there are some differences read about it on "mdn web docs" or "javascript info". So what does promise contain? it contains data in form of json. It may contain resolved data or rejected data. A resolved data can further be converted into javascript object which we can use in javascript as we use any other object. basicalyy try to remember this flow:

    fetch or any async task --returns--> promise --> any action on promise is performed by .then function --> any error while fetch is caught by .catch function.

    @initarsh

    Arsh Ali

    @initarsh

    5

    Let's try to understand everything in layman terms:

    JavaScript is synchronous, single threaded language. It can Just do one thing at a time, it has just one call-stack and it can execute one thing at a time. Whatever code we give to Javascript will be quickly executed by Javascript engine, it does not wait.

    But what if we have to delay execution of any line? we could utilize callback function.

    console.log("Welcome to "); setTimeout(function () { console.log("Codedamners"); }, 5000); console.log("Codedamn"); // Welcome to // Codedamn // Codedamners

    // Here we are delaying the execution using callback approach of setTimeout. Async programming in JavaScript exists because callback exits. But is using callback an efficient solution to handle async operations in Javascript ? There are two major issues of using Callback to handle async operations in JS :

    1. Callback Hell (aka Pyramid of Doom): When we have a large codebase and multiple apis and have dependency on each other, then we fall into callback hell. These codes are tough to maintain.

    E.g : api.createOrder(cart, function () { api.proceedToPayment(function () { api.showOrderSummary(function () { api.updateWallet(); }); }); }); // Callback Hell

    1. Inversion of Control : Inversion of control is like that you lose the control of code when we are using callback. E.g:

    api.createOrder(cart, function () { api.proceedToPayment(); });

    // So over here, we are creating a order and then we are blindly trusting createOrder to call proceedToPayment. // It is risky, as proceedToPayment is important part of code and we are blindly trusting createOrder to call it and handle it. // When we pass a function as a callback, basically we are dependant on our parent function that it is his responsibility to run that function. This is called inversion of control because we are dependant on that function. What if parent function stopped working, what if it was developed by another programmer or callback runs two times or never run at all.

    Now, let's understand what promises are and why do we use them. So, What is a promise? According to mdn web docs, A Promise is an object representing the eventual completion or failure of an asynchronous operation. Promises are used to handle async operations in Javascript.

    Q: How it is better than callback approach ? In Callbacks we used to pass the function and then used to trust the function to execute the callback. But with promise, we are attaching a callback function to a promiseObject. There is difference between these words, passing a function and attaching a function. By attaching a function we get a guarantee that it will be executed (that guarantee is given by JS) , and control still remains in our hand (as we can make changes in our functions anytime) , so no case of Inversion of control. To avoid promise hell (promise under promise ...) we use promise chaining or nesting promises concept.

    @vishalkr17

    Vishal Kumar

    @vishalkr17

    0

    The previous .catch solution was really weird and the solution code wont run locally. Insight would be appreciated

Your comment