i have question about Promises on something that is really confusing me.
The .then() method
before i will get into the thing that confusing me about the .then() method i would do a brief explanation on how the Javascript Engine works based on my knowledge.
From what i know Javascript is not a asynchronous but rather a synchronous language
The Javascript Engine works synchronously but the Javascript Engine isn't the only thing that run on the Browser
There are things like the Rendering Engine,setTimeout,HTTP Request etc
And the Javascript Engine can talk to them when we are calling a native functions for example
setTimeout
so the setTimeout
function will call a program outside of the Javascript Engine for timing
And of course when the timer will end it will send the callback to the event queue and only after the Javascript finished all the Executions context only then it will look at the event queue
Alright now let's move to Promises
My question is how .then()
knows when resolve()
was called
I have read articles and they say that .then()
works asynchronously which is sounds weird to me because Javascript is synchronous doesn't it ?
maybe i didn't understood them correctly
so i made my own assumptions on how .then()
works because i haven't found a source that gave me the feeling and confidence that i know exactly how .then()
works.
one of them(my assumptions) was that there are two stages in the .then
method
i will use this code for demonstration to explain my assumption
var myPromise = new Promise(function(resolve){
resolve('Hello Stackoverflow !');
});
myPromise.then(function(result){
console.log(result);
});
so based on my assumption the resolve('Hello Stackoverflow !')
function calls the .then
method and the
.then
check two things here are the following
1.if the callback parameter of .then()
was created
2.if the Promise status is set to resolved
and if both conditions are true then the .then()
method will insert the callback with the value Hello Stackoverflow !
to the event queue and only after all the execution context popped of the stack then it will run the callback and we will get the result Hello Stackoverflow !
again this is based only on my assumption maybe i'm totally wrong about that.
So if you examine what i just said you can reach a conclusion that the .then
method is called twice
Why ?
the first time is when the resolve function called it but not all conditions were true
the one that isn't true is that the .then
callback parameter was created but it wasn't created yet
because we haven't got to the line of code that we created the callback so the condition is false
and the second time is when we called the .then
method and created the callback and all conditions were now true so it will insert the callback to the event queue and after all the execution contexts will popped of the stack then the callback will be called
and we will get Hello Stackoverflow !
hope you understand what i tried to explain
Am i right or wrong ?
Thanks in advance for all of you :)