Is it true that the resolve()
function and then()
function below are the only functions in the ES6 Promise mechanism that look at and add the fulfillment handlers to the job queue (and mark them as already added and never again)?
let promise = new Promise(function(resolve, reject) {
console.log("resolve, reject are", resolve, reject);
setTimeout(function() {
resolve("this is the success data");
}, 1000);
});
promise.then(
function(a) {
console.log("Success.", a);
promise.then(function(a) { console.log("Go have a drink.", a); });
},
function(a) {
console.log("failure", a)
}
);
An alternative to the above code is, the resolve()
is done directly, without being in the setTimeout()
. Is it true that the then(fn)
will add the fn
to the fulfillment handler table (not job queue) for this promise, or if the promise has already been resolved, directly add the fn
to the job queue, and the fn
is marked as "already added to job queue" and no need to be added again?
On the other hand, the resolve()
is a JS ES6 provided function, that mark a promise as "resolved" (state), and check if the fulfillment handler table for this promise is empty or not. If there are such handlers (and should not have been marked already added to the job queue before), then these handlers are added to the job queue and marked as already added.
And in any event, suppose there are fn1
, fn2
, fn3
, ... etc in the fulfillment handler table, each one is added to the job queue as fn1(resolvedValue)
, where the resolvedValue
is remembered internally by the resolve(resolveValue)
call?
And there are no other situation where the fulfillment handler table is accessed at all. The job queue jobs are executed by the JS event loop, so that when all the GUI "click event handlers", etc are done, then the fulfillment (and rejection) handlers in the job queue are executed, and then also the setTimeout and setInterval handlers are also executed? Is this how the fulfillment handler table and job queue work?