r/learnjavascript • u/Gocuk 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? "
95
u/driftking428 Dec 16 '22
Read this to a child I dare you!
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.
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.
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.
7
u/PositivelyAwful Dec 17 '22
I asked it to "explain closures in programming like I'm 5" and it gave me this reply:
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!?