javaScript promises explained tutorial




How Promises in JavaScript work and how to use them. Explained in a very simple language with real world example.

JavaScript promises example
How to use promises in JavaScript

How to avoid callback hell, advanced javascript tutorials , Java Script, Callback functions , techsith, techsittube

Original source


36 responses to “javaScript promises explained tutorial”

  1. But how can I extract a result from the final "then"? Your final "then" is a consumer – a 'void' function if you will. What about "let thing = new Promise((rl,rj) => rl("hello")).then()" // 'thing = "hello"

  2. When you create an instance of type Promise, the constructor takes one function which has two callbacks. The body of the function is provided inline to clean the room and invoke callback accordingly. At this point, the function body is not executed, right?

    If I am right, when is it executed? I think you said the promise is executed at line promiseToCleanTheRoom.then(…..).catch(……). How? To me, it looks like that line is just meant to wire the 'then' event to a function, and wire the 'catch' event to another function.

    My question is: who actual starts running the promise?

  3. I'm a newbie. BARE WITH ME 🙂
    i created an array which have some reserved strings which means *true*, (yes that's how i want this script to run.)
    Here's the code,
    *********************************************************************************************************
    let promiseToCleanTheRoom = new Promise(function(resolve, reject) {

    var i, arr, abc;
    arr = ["notfalse","true",1]
    abc = prompt();
    for (i = 0; i <= arr.length; i++) {

    if (abc != arr[i] ) {
    reject(' not Clean');

    } else {
    resolve(' Clean');
    break;
    }
    }

    });

    promiseToCleanTheRoom.then(function(fromResolve) {
    console.log('the room is' + fromResolve);
    }).catch(function(fromReject) {
    console.log('the room is' + fromReject);
    })
    **********************************************************************************************************
    PROBLEM :The script only reads a[0] as true, and everything else in the array as false.
    THANK YOU
    PS :- I kinda figured out the problem, so the problem is with i in for loop, if the i is set to 0 it reads "notfalse" as true when i = 1 it reads "true" as true.but i still dont know why i isnt increasing..
    ——————————————————————————————————————————————————————
    do {
    abc = prompt();
    if (abc == arr[0]||abc == arr[1]||abc == arr[2]||abc == arr[3]/*….[n]*/) {
    resolve(' Clean');

    } else {
    reject(' not Clean');
    break;
    }
    }
    while(resolve()=true);
    (THIS WORKS BUT I WANT TO DO THE SAME WITH FOR LOOP).

  4. This was good for teaching the mechanics of promises but is horrible at teaching what they are intended for – an alternative syntax to nested callbacks for async communication with the outer environment ( browser, node etc…)

Leave a Reply