Isn't promise asynchronous?

Why the result of running is cdab instead of cabd

function a(){console.log("a");}
function b(){console.log("b");}
new Promise(function(resolve,reject){
    console.log("c");
    resolve(22);
}).then(console.log("d"));
a();
b();

// 
c
d
a
b
Sep.26,2021

new Promise((resolve,reject) => {
    console.log("c"); 
    resolve("d")
 })
   .then(res => {
    console.log(res)
 })
a();
b();

then should accept a function;
here directly use the result undefined of console.log ("d") as its callback. Of course, the registration process is Synchronize.
do not misinterpret the then method as an asynchronous method. Then callback is asynchronous. The
then method immediately returns a new promise object, at which time the new promise object already has a then callback, and waits for the last promise state to change from pending to execute the callback


promise is a new object of ES6, mainly to solve the callback hell and solve the problem of asynchronism.


where is your code asynchronous?


because of your There is no asynchronous code.
promise is not asynchronous code, but solves the problem of asynchronous callback, so it is used with asynchronous code.
promise itself does not have asynchronous behavior.


modify the contents of then and try again:

.then(()=>{
    console.log('d');
})
The

then method receives a parameter of Function type

Menu