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”
o wow nice explanation. love your tutorial. 😀
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"
I've been working with platforms like Firebase that uses promises and never fully understood the concept until now. Thanks for the tutorial
Very helpful!
You cleared my ideas. Thank you =)
Awesome video… Great job @techsith
Thanks for such a well explained video 🙂
Thanks , its quite helpful for beginners to understand promise . 🙂
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?
When you use Promise.all(……), how do you get all the strings (e.g Cleaned The Room, remove Garbage, won Icecream) from each function?
Thank you! Excellent job.
"I promised this tutorial and I resolved it" Oh man :'> <3
i like it
Thx for the very nice video 🙂
I understood everything and it was exactly what I needed in order to move forward in my JS project.
Thanks again!
helpful, thanks!
if only everyone explained so well, great work!
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).
it is so good i just had to share it… already liked and subscribed… keep it up… 🙂
very nice ..Thanks! btw which one is better in your opinion then(fulfillment, rejection) or then().catch() ??
Awesome!!
It was really great. Thanks
I sometimes have trouble learning from youtube videos, but this was a well done tutorial and I had no trouble at all! Thanks!
it is much simpler and easier to understand, that I have seen for JavaScript promises. Thanks Man (y)
very nice explained. thank u.
very clear and useful to a js beginner!
i like this video after 3 seconds .this guy is true teacher
great tut, thanks man 😉
You have great skill, to explain complicated thing in simple way , what you promised you delivered ,I gone through the udacity-video also, but your explanation matters !
Thanks !
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…)
Can u please explain how to write catch blocks for second example
Great. Really helped.
Loved the video. Great lesson. Good quality of video and great explanation. Thanks
Brilliantly explained!
Highly simplified demo. Thanks 🙂
At exactly 3:15 of this video. That arrow you made. That's when I got promises. Thank you. Love your channel.
Thank you very much.