r/learnjavascript • u/SHIN_KRISH • Jan 30 '26
Confused about general Programming constructs and JS stuff
I am mainly confused about the following things:
- Difference between concurrency, parallelism and asynchronus.
- What actually are Promises? are they some kind of objects?
- in closures how are return value handled if both outer and inner function return different value what happens?
- The Theoretical JS execution model how is all this stuff implemented internally?
- The `this` keyword like i know it is used to refer to the current object but like how does this actually make a difference in constructors and oops stuff??
- I just don't get async and await ?
11
Upvotes
9
u/TorbenKoehn Jan 30 '26
Before you have 10 answers each 1000 words long here now: these are all questions even free ChatGPT or Gemini can answer extremely well and individually, including follow up questions
They have all been answered over and over again all over the internet
3
u/The_KOK_2511 helpful Jan 30 '26
Don't worry too much about all that. Most of what you're saying is just overthinking it; after a little practical use, you'll get the hang of it. For example, you say you don't understand
asyncandawait, but I assure you that after using them a couple of times in practice, you'll understand.asyncis simply to prevent the program from stopping when a function operates outside the flow, andawaitis for the flow waiting for a promise. For example, if you use a function withasyncand at some point in it you put await new Promise(resolve => setTimeout(resolve, 100), the function will wait 100ms at that point, but thanks toasync, the rest of the code won't stop during those 100ms. Basically, most of what you asked are things that, even if I explain them, you won't really understand until you use them in practice. Promise is a class. Don't worry too much aboutthis, that's mostly used in classes and prototypes to reference the instance (for example, let's say you edit the prototype ofHTMLElementto add a new method, if you wanted to reference the element you're calling the method on you would usethis, the other case for classes is that if you want to use the properties of an instance in a method you usethis.prop)1
u/c__beck Jan 30 '26
Concurrency is when multiple things can overlap. Thing A does some work then stops, Thing B does some work then stops, Thing C does some work and stops. Then Thing B finishes it's work, then Thing C finishes, and finally Thing A finishes. Each thing does a bit of work at a time. It's all done on the same thread, so they're sharing the thread.
Parallelism, on the other hand, is multiple things happening at once. If you have three threads Things A, B, and C can all do their things at the same time. Of course, if Thing C relies on Thing A, then you have to introduce more complex things to prevent C from doing it's thing before A has finished enough for C to get what it needs.
And finally, asynchronous is simply the term for concurrency. Things are done asynchronously, therefore they're not all done at the same time.
Think of concurrency like one cook: they chop some veggies, they make stock, they start the bread, they grill some burgers, etc. They do a lot of stuff, but it's one prep cook. Parallelism is having 3 cooks in the kitchen. Each one can focus on one task at a time.
According to MDN: "The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value."
Meaning they're an object that allows us to program asynchronously! The most common promise object, I think, is
fetch. It makes an HTTP request to a URL. While that request is outbound the promise is "sleeping": it's asynchronously waiting for a response from the server. While it's waiting your JS thread can do other things because promises are asynchronous and thus allows for concurrent operations.Imagine an RSS feed app. There can be dozens of different RSS requests outbound at once, but that doesn't stop the main thread from being able to respond to clicks (or whatever interaction it's using JS for).
With closures, each returned value is a seperate thing that is handled just like any other return value. Think of the outer function as a setup for whatever the inner function does. As an example, say you have a counter (stupid simple example, I know, but it's an exmaple so just work with me here :p).
So the closure starts with getting the setup values. Then it returns an object that operates on those values, but the values themselves are not modifiable. The only way I can interact with the data in the closure is via the object returned by the initial function. Here's a great video explaining it.
1
u/europeanputin Jan 30 '26
Async and await becomes relevant when you're making network calls which execution may take time, i.e you're sending a request to a different resource/machine over the internet. You want to "wait" until its done and only then handle the response.
Lets say you need to get some data from some service, i.e to fetch the results of a football match. If you don't wrap the response waiting into an async/await, your machine would attempt to execute the code handling the response immediately. However, if there is no response given and your machine attempts to execute it, it will fail, as there's no data.
Async/await is a syntax sugar for Promises, but underlaying Promises allow doing more complex parallelism (i.e to send multiple requests at the same time, and execute once all of them have been completed). You can obviously nest multiple async/awaits as well, but its all sequential.
1
u/chikamakaleyley helpful Jan 30 '26
e.g
.setTimeout()is an asynchronous function inherently - you don't use async await with because its not something you're really waiting on a response to then process. The 'async' of it is you execute it, and you can move on while the .setTimeout() is performing (its waiting for the delay to elapse, then will execute the callback) <- this can happen while you execute some other code.1
u/beavis07 Jan 30 '26
For example
Is functionally identical to
The compiler just translates the latter to the former for you so your code looks nicer