I have been trying to understand promise under the hood. There is lot of source code online. I tried but could not understand the chaining part. I understand the recursion is used for chaining but still, is there any resemble data structure or algorithm which i can head around and then trying promise under the hood.
Asked
Active
Viewed 44 times
1
-
What do you mean by "under the hood"? It is too deep to say such things in programming. – canbax Apr 30 '21 at 13:59
-
I meant how it works(under the hood), the libraries which implements promises, i am obstinate to understand them, i am unable to move on. Help – bikashamit Apr 30 '21 at 14:02
-
1@canbax It's normal to say "under the hood" in programming. – FZs Apr 30 '21 at 14:04
-
the whole story is about async code execution vs sync code execution in JS. JS is single threaded. It is using event loop mechanism. https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop take a look at that. Also, read other docs in MDN about promises – canbax Apr 30 '21 at 14:05
-
@FZs it's normal in everywhere but not descriptive for this context for me at least, – canbax Apr 30 '21 at 14:06
-
1@bikashamit Do you want to understand how are they implemented, or just how do they work? Please try to focus on a specific question: what don't you understand about the algorithm? Which is the part that throws you off? What code (or algorithm or whatever) did you look at? – FZs Apr 30 '21 at 14:07
-
@bikashamit You might get better answers if you ask about a specific part of the code in a specific library that you don't understand. Otherwise, I can only point you to [How is a promise/defer library implemented?](https://stackoverflow.com/a/17724387/1048572) or [Concept - Distilling how a promise works?](https://stackoverflow.com/q/15668075/1048572) – Bergi Apr 30 '21 at 14:08
-
@FZs how they implemented, how they propagte information to chained promise? I have tried some DS but not much. Search trees and some dynamic programming but have never got these type of complexation. So some DS which i should try before moving to these promise under the hood. – bikashamit Apr 30 '21 at 14:14
-
1@bikashamit What is a DS? And neither search trees nor dynamic programming have anything to do with promises. – Bergi Apr 30 '21 at 14:22
-
@Bergi I think it stands for *data structure*, but I might be wrong... – FZs Apr 30 '21 at 14:23
-
1@FZs Ah, likely. In that case: Promises are not a "data structure", they simply are stateful objects implementing something like the observer pattern; there is no "algorithm" involved to get a particular result. If you want to get at theory, they (nearly) form a monad - but I doubt that'll help you. – Bergi Apr 30 '21 at 14:25
-
I meant DS(data structure). Yes observer pattern. They used some queue data structure then recursion.The chaining how did they do it. The propagation of information to ladder how did they do? Was that adhoc? – bikashamit Apr 30 '21 at 14:28
-
Sorry good people if have made you bit off. I am stuck, i am at my remote village, i have no connection to ask any one. This was the only place i could have asked. May be i will try more harder. May be it will take more time. Thank you again. – bikashamit Apr 30 '21 at 14:36
-
@bikashamit If by chaining you refer to `.then(…).then(…)` syntax, that's just an artifact of the method returning another promise instance. (Which was desired in the design, of course, but is nothing specific to promises). Not sure what you mean by "ladder". – Bergi Apr 30 '21 at 15:07
-
@bikashamit, the word by **_recursion_** mean loop, But chaining is not **_recursion_**. So **_recursion_** is not used for chaining. – Nur May 01 '21 at 09:22