How to understand resolve (what is executed inside Promise.resolve ())

new Promise((resolve, reject) => {
  console.log("async1 start");
  console.log("async2");
  resolve(Promise.resolve());
}).then(() => {
  console.log("async1 end");
});

new Promise(function(resolve) {
  console.log("promise1");
  resolve();
}).then(function() {
  console.log("promise2");
}).then(function() {
  console.log("promise3");
}).then(function() {
  console.log("promise4");
});

the execution result is as follows:

async1 start
async2
promise1
promise2
promise3
async1 end
promise4

< hr >

ask the Great God to explain why the result is like this.

Oct.26,2021

Promise.resolve () returns a new Promise object. In this case, the outside resolve will not directly generate then, but will check the Promise, resulting in a delay


.

are you sure your execution result is correct? I executed it under chrome, and the result is as follows:

async1 start
async2
promise1
async1 end
promise2
promise3
promise4

this result makes sense. The constructor of Promise will be executed immediately, and the callback function of resolve will be placed in job queue to wait for the completion of the director queue. Please refer to my article

.


chrome 70.0.3538.77

firefox:

firefox 63.0.1


resolvepromise.resolve()Promise



Chrome Node

TypescriptPromise

clipboard.png

resolvePromiseLikethenable,then

resolvethenablethenablePromisethenablePromisePromiseLike

clipboard.png


clipboard.png

A-B-A-B

clipboard.png

clipboard.png

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3098e-2bd0d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3098e-2bd0d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?