DEV Community

Discussion on: Asynchronous Javascript - 04 - Promises

Collapse
 
ilya_sher_prog profile image
Ilya Sher

The function contains two arguments, resolve and reject.

Functions don't contain arguments. This is semantically incorrect. Functions have parameters.

output of the asynchronous operation.

"result" would be way better than "output" to avoid confusion with output such as console.log()

The first one gets executed when resolve is called in the executor function. The second callback function is executed when reject is called in the executor function.

Not if the Promise is already settled. In that case the callbacks will be scheduled independently of calls to resolve() and reject().

adds all the functions from the microtask queue into the call stack

This phrasing suggests piling functions in the call stack one on top of another. This doesn't make sense.

The above code creates a promise to generate a random number from 1 to 10

Nope. Even after you fix the typo Math.ceil(Math.random * 10); => Math.ceil(Math.random() * 10);, the result is 0 to 10, because Math.random() can return 0.

P.S.

In my previous comments I was heated after commenting on blatantly poor code ( dev.to/ilya_sher_prog/comment/m5f7 ) so the tone could be better.

Collapse
 
kabir4691 profile image
Kabir Nazir

Uptil now, I thought that arguments and parameters were the same. But I just looked them up on the web again and understand the difference now. Shall correct it.

I used the word 'output' instead of 'result' because I didn't want the reader to confuse it with the 'result' property of the promise. I have even clarified what I mean by output in the next sentence, to be more clear.

Regarding the execution of the callbacks, I didn't mention about the condition you spoke of, because I wanted to keep this article simple without diving too much into the specifics. However, I did add this paragraph, which should give the readers more clarity. Please let me know if it still doesn't clear any misconceptions. I shall edit more, in that case.

Note that in a promise, the executor function can call only either resolve or reject once. Any subsequent calls to either resolve or reject after the first one are ignored. This is because a promise is supposed to have a single result of either success or failure.

Regarding the call stack, I was indeed of the idea that functions do get added and piled into the call stack from the microtask queue. After all, that's why it's called a stack, right? Please correct me if I am wrong, hopefully with some online resources to clear my concepts.

You're right about the random function. I was initially of the impression that the value returned is from 0 to 1, both exclusive. I shall correct that as well as the typo.

P.S.
I apologize for losing my cool as well in the reply to your comment. I understand that you don't want articles with half baked information floating around on the Internet that can confuse beginners. But, you also need to understand, that as a beginner, everyone is bound to make mistakes. When that happens, it's up to senior devs like you to correct and guide the junior ones. If there was no room for making mistakes, then platforms like Dev and Medium wouldn't exist in the first place. All that would exist is MDN or some other official documentation and no one else would be allowed to discuss concepts on the web. In the pursuit of ensuring quality, one must take care not to be too harsh on newbies that it totally discourages them from writing at all. Hope you understand.

Collapse
 
ilya_sher_prog profile image
Ilya Sher

I thought that arguments and parameters were the same

You are welcome!

I used the word 'output' instead of 'result' because I didn't want the reader to confuse it with the 'result' property

I see. Not sure what would be best here. output can be confused with I/O.

Regarding the execution of the callbacks, I didn't mention about the condition you spoke of, because I wanted to keep this article simple without diving too much into the specifics.

I see... but this phrasing suggest that resolve() must happen after .then(...) is attached. This is incorrect.

functions do get added and piled into the call stack from the microtask queue.

I am not checking the source code but chances are call stack is used like in many other languages meaning it contains call frames (created by calling a function and removed by returning from a function) one on top of each other, not arbitrary unrelated code. This should be checked.

You're right about the random function. I was initially of the impression that the value returned is from 0 to 1, both exclusive. I shall correct that as well as the typo.

I would rewrite the random example because despite the 0 to 10 range, the chances of getting exactly zero are very slim. That's why code using random numbers typically floors and not ceils the numbers.

floating around on the Internet that can confuse beginners

Huge concern. Later, I need to work with these people.

But, you also need to understand, that as a beginner, everyone is bound to make mistakes.

That's optimistic. Not only as beginner.

senior devs like you to correct and guide the junior ones

In my perfect word, that happens somehow in an organized manner before publication to avoid people learning from possibly incorrect un-reviewed content. How realistic is that? Probably not that much.

discuss concepts on the web

Mmm.. discussion is fine but articles here are presented as information (I guess assumed by beginners to be correct), not as discussion.

harsh on newbies

Just making it clear: nothing particular against newbies :) Just correctness. You can see I opened issues on GitHub for Rust book and C++ documentation at Microsoft. I have no idea who did that but probably not beginners.

Thread Thread
 
ilya_sher_prog profile image
Ilya Sher • Edited

I am not checking the source code

Did check. It had reference to html.spec.whatwg.org/multipage/web...

The relevant part is:


While the event loop's microtask queue is not empty:

  • Let oldestMicrotask be the result of dequeuing from the event loop's microtask queue.
  • Set the event loop's currently running task to oldestMicrotask.
  • Run oldestMicrotask.
  • Set the event loop's currently running task back to null.

In light of this, you should also review your previous articles in series because the call stack is mentioned all over there.

Thread Thread
 
kabir4691 profile image
Kabir Nazir

but this phrasing suggest that resolve() must happen after .then(...) is attached

That wasn't how the article was phrased. I did attach code examples below it to demonstrate it better. However, I have still edited the post to make it more clearer.

I would rewrite the random example

I agree. I shall edit accordingly.

call stack is used like in many other languages meaning it contains call frames (created by calling a function and removed by returning from a function)

I'm aware of the concept of call frames. But if I had to mention that in my previous articles, I would have to go even much deeper and that could make the post very long. My aim of the articles was to give a basic but sound explanation of concepts without diving too much into advanced concepts or specifics.

articles here are presented as information (I guess assumed by beginners to be correct), not as discussion.

I think it's safe to say that it should be common knowledge for readers that articles on Dev.to are written by a myriad of people (ranging from beginner to experienced) and not by the people who actually developed the technologies. Hence, while the articles can be used as a source of information, they shouldn't be necessarily taken as a gospel of truth. The official documentation is always where any developer should consult first for precise and deep knowledge.

The same goes for other corrections you have mentioned in my post. Getting into all the details for a specific topic isn't what Dev or Medium is about. This is a blogging website, not an official documentation source. But I do understand your concern regarding the correctness of the information. What I can do is add a link at the bottom of the post redirecting the user to the official documentation (like MDN), where they can explore more about the concepts in detail.

Some comments have been hidden by the post's author - find out more