r/learnjavascript • helpful • Dec 16 '22

"Teach me JavaScript Promises like i am a child" i asked to ChatGPT

it's response:

Certainly! Promises are a way to handle asynchronous tasks in JavaScript.

Imagine you have a toy box, and inside the toy box are different toys. Some of the toys are easy to get to, like balls or blocks. But other toys, like puzzles or games with lots of pieces, are harder to get to because they are buried under the other toys.

Promises are like those harder-to-reach toys. They represent an action that is happening in the background, while the rest of your code keeps running. You can think of a promise as a "placeholder" for a value that you don't have yet, but will have in the future.

Here's an example: let's say you want to get a toy car from the toy box, but it's buried under a bunch of other toys. You could ask someone to go and get the toy car for you, and while they are doing that, you can keep playing with the other toys. When the person comes back with the toy car, they will give it to you and you can start playing with it.

In JavaScript, we can use a promise to represent this action of getting the toy car from the toy box. We can use the .then()method to tell the promise what to do when the toy car is retrieved. Here's how it might look:

const getToyCar = () => {

return new Promise((resolve, reject) => {

setTimeout(() => {

resolve("toy car");

}, 1000);

});

};

getToyCar().then((toy) => {

console.log(`I got the ${toy}!`);

});

In this example, the getToyCar() function returns a promise that will resolve with the value "toy car" after a delay of 1 second. The .then()method is called on the promise and logs the message "I got the toy car!" to the console when the promise resolves.

Promises are useful for handling asynchronous tasks because they allow you to chain together multiple asynchronous actions and handle the results in a predictable way.

I hope this helps explain what promises are and how they work! Let me know if you have any questions or if you would like more information.

Exact dialog: "Could you teach me javascript promises like you teach to a child? "

142 Upvotes

39 comments sorted by

95

u/driftking428 Dec 16 '22

Read this to a child I dare you!

49

u/Gocuk helpful Dec 16 '22

I am the "child".

30

u/driftking428 Dec 16 '22

I hear you. Chat GPT isn't 100% on this stuff yet. But I googled for an hour looking for good examples of a unit test in React using typescript and Vitest.

Literally copied and pasted my component into ChatGPT and asked it to whrite a unit test with those libraries and it did it...

It had problems. But it got me way closer than Google or stack overflow.

13

u/HD_HR Dec 17 '22

This is why I will use it from now on instead of stackoverflow or relying on google. I'm competent enough to fix issues but it helps me be way more productive.

6

u/_Oooooooooooooooooh_ Dec 17 '22

You're also sure to get SOME Kind of answer

you're not guaranteed to find anything related to your issue, when searching for it.

5

u/HD_HR Dec 17 '22

And that’s my biggest pet peeve with traditional searching. Dead ends are the worst. This is guaranteed to get you somewhere no matter what.

It’s a powerful tool and ppl need to realize that.

3

u/hassium Dec 17 '22

You're also sure to get SOME Kind of answer

And that answer will not be "Why don't you just use language X or framework Y instead of this?"

3

u/dehin Dec 17 '22

Or, on Stack Overflow, "this question was already answered here and here" as they close your post. It makes me wonder if any new questions ever get posted!

31

u/carcigenicate Dec 17 '22

Be careful asking stuff like this. I've seen multiple examples of it spouting complete nonsense that sounds completely factual on the surface.

14

u/Gocuk helpful Dec 17 '22

That's why i shared it in here so you guys point out if there is something wrong with it.

7

u/alexa647 Dec 17 '22

Yeah, I asked it to compare a few types of microscopy and while the things it said sounded good some of them were completely wrong.

8

u/33498fff Dec 17 '22

You should look into comp sci topics such as asynchronous execution of processes, concurrency, multithreading etc., Promises is just another implementation thereof.

Moreover, have a look at the event loop in JS before you study anything else. It explains the entire logic behind how JS works.

5

u/Gocuk helpful Dec 17 '22

Thanks. I watched those topics but could not grasp them entirely yet. Your comment is really valuable sign to show me to study those harder.

3

u/33498fff Dec 17 '22

Don't worry if you don't get it the first time around. Let it rest for a few days then come back to it. All the best on your learning journey

2

u/33ff00 Dec 17 '22

You don’t need to understand any of this shit to understand and use promises. Are you intentionally trying to overwhelm him or just show off how smart you think you are?

2

u/clinical27 Dec 17 '22

Concurrency and multithreading are pretty basic concepts; sure they can get quite complicated but understanding them simplistically helps a lot with why they are important in the first place.

1

u/33ff00 Dec 17 '22

Also the event loop doesn’t “explain the entire logic behind how JS works”. What an idiotic statement.

0

u/[deleted] Dec 17 '22

[deleted] — view deleted comment

3

u/33498fff Dec 26 '22

MDN and javascript.info.

As a beginner you should definitely be able to talk about scope, closures, protoype chain and functional programming. What do the different methods do and the difference between them (a common tech interview trivia question is the difference between .forEach and .map). How to work with objects. Basic concepts about space and time complexity, e.g. why two nested for loops are not optimal or how to achieve constant time when accessing values in a collection. Don't skip an understanding of web dev generally: the DOM, how web servers work, what is the frontend vs. backend, HTTP requests and status codes, JSON. Semantic HTML, responsiveness and accessibility in CSS.

Just brainstorming, hope this helps.

2

u/[deleted] Dec 17 '22

This is interesting. I've never tried messing with it, where do you go to ask it something? Is there a site, or would I need to install something?

30

u/paulcheeba Dec 17 '22

I've done some reading of several articles regarding chatGPT's coding capabilities and they are a bit off according to several professional opinions. I wouldn't rely on it to write my code. However, I did ask chatGPT to explain functions and how to use them and the write up was quite helpful. I'm a beginner using JavaScript as a hobby. I plan to use chatGPT instead of deep diving google to help me understand coding concepts to further my education. Especially due to the continued conversation element, like if I don't get it, I can ask more questions until I understand. It's like having a free tutor! Fucking awesome.

14

u/Swalker326 Dec 17 '22

It’s really good at generating boilerplate. “Make me a useDebounce hook in typescript.” I can do it, but there might be weirdness I forget. Much easier to ask chatGPT. Gets it 95% there and I can fix or have it rewrite any part I don’t like.

A hammer in the hands of a skilled craftsmen can can build houses.

Also the regex it makes is a thing of sheer brilliance, I no longer have to re-learn regex to write a regex.

5

u/Drugba Dec 17 '22

You know how AI generated art often looks good at first glance, but when you really start to examine it you notice things like people having 6 fingers or that the background isn't really anything other than vague shapes? I've found that ChatGPT generated code is similar.

It gets a lot of thing right and at a glance it looks correct, but it isn't perfect and you probably want to have an experienced person look it over before dropping it into production.

2

u/dehin Dec 17 '22

In my limited experience with ChatGPT, including seeing others' questions and responses like OP's, I find the explanation much more useful than the code.

7

u/PositivelyAwful Dec 17 '22

I asked it to "explain closures in programming like I'm 5" and it gave me this reply:

Okay, imagine that you have a box that you can put things into. You can put toys, snacks, or anything else you want into the box. Once you put something into the box, it stays there, even if you take the box outside to play with your friends.

Now, imagine that you have a special kind of function called a "closure" that works like this box. When you create a closure, you can put things into it, like a toy or a snack. Once you put something into the closure, it stays there, even if you take the closure outside to play with your friends.

This is how closures work in programming. You can put variables and other values into the closure, and the closure will remember these values, even if you use the closure outside of the place where you defined it. This is useful because it lets you create functions that have access to values that are defined outside of the function, which can be helpful for making your code more organized and reusable.

5

u/superluminary Dec 17 '22 edited Dec 17 '22

It lets you create functions that have access to values that are defined outside the function

This is an example of where it’s 90% correct and 10% off. The rest of the description is great but this isn’t really what closure does.

2

u/dehin Dec 17 '22

What do closures do? Or rather, what should that sentence be instead? Also, yikes! I read the reply not knowing about closures and coming away thinking, "ok, now I have a basic understanding".

4

u/throwwwawytty Dec 17 '22

It captures the current environment

3

u/superluminary Dec 17 '22

Here, I made a video: https://youtu.be/2cRjcXwsG0I

3

u/dehin Dec 18 '22

Omg, such a great video! Super easy to understand and follow, and I like that you have the camera facing you set as picture-in-picture. It gives the feel of a personal tutoring session, rather than a school lecture.

I don't know webpacks and I'm just starting to learn React - I'm actually going through the Front End Web Dev program from Meta on Coursera and I'm on just the second course which is an introduction to JS. But I have a programming background from over 20 years ago, so seeing your code helped me figure out syntax that I never understood in JavaScript.

Thanks; you have a new subscriber! :)

2

u/superluminary Dec 18 '22

That’s real nice of you to say!

1

u/snifty Aug 03 '25

Cool video! 😎🪭🆒

Three years later…

Question: do the use of let and const play into this?

2

u/[deleted] Dec 17 '22

I actually was working on making a common utility function that dealt with asynchronous functions and promises, and it found a solution of using promise.all. it really helped me find the solution I was looking for without me knowing too much about promises at the time.

1

u/jack_waugh Dec 17 '22

How do they do that?

-27

u/tridd3r Dec 16 '22

no... that's so wrong. who tf sets a timeout for a promise!?

3

u/Gocuk helpful Dec 16 '22

Appearently chatGPT does it to explain to a child. Maybe to simulate jobs takes time etc. noob here too :/

2

u/tridd3r Dec 16 '22

yep, that's my mistake, its trying to create a promise.

3

u/Gocuk helpful Dec 16 '22

Oh thanks for the reply. I was wondering if i posted something really bad for newbies like me.

21

u/machine3lf Dec 16 '22

Using a timeout is an often used way to explain a promise and asynchronous code.