Promise, async and await interview questions

Promise, async and await interview questions

1. About the execution order of Promise

Create Promise are synchronized, when executed finished resolve();, the state is changed resolved, the latter .thenimmediately placed microqueues, the second .theninto the first .thenreturn promise object buffer queue (not microqueues).

1.

new Promise((resolve, reject) => {
    
    
  console.log("外部promise");
  resolve();
})
  .then(() => {
    
    
    console.log("外部第一个then");
    return new Promise((resolve, reject) => {
    
    
      console.log("内部promise");
      resolve();
    })
    .then(() => {
    
    
    console.log("内部第一个then");
    })
    .then(() => {
    
    
    console.log("内部第二个then");
    });
  })  这里返回的对象是最后一个.then返回的对象,第一次时状态为‘pending’
  .then(() => {
    
     所以第一次这里会缓存起来并不是加入队列
    console.log("外部第二个then");
  });
 

2.

new Promise((resolve, reject) => {
    
    
  console.log("外部promise");
  resolve();
})
  .then(() => {
    
    
    console.log("外部第一个then");
    new Promise((resolve, reject) => {
    
    
      console.log("内部promise");
      resolve();
    })
      .then(() => {
    
    
        console.log("内部第一个then");
      })
      .then(() => {
    
    
        console.log("内部第二个then");
      });
  }) 这里返回的是undefined 的promise对象
  .then(() => {
    
     第一次时这个promise对象已经是resolved了,所以立即加入队列
    console.log("外部第二个then");
  });

3.

console.log(1);
setTimeout(() => {
    
    
    console.log(2);
});
new Promise(resolve => {
    
    
    console.log(3);
    resolve('resolve');
    console.log(4);
    reject('error')
}).catch((err) => {
    
    
    console.log(err);
}).then((res) => {
    
    
    console.log(res)
});
Promise.resolve().then(() => {
    
    
    console.log(5);
});
console.log(6);

The implementation of catch, the state has become resolved, it will not catch the callback function to perform, and perform a successful callback function default: onResolved : value => value. It must be noted that the callback function of catch is directly replaced here, so it will not be executed console.log(err);again.

1
3
4
6
5
resolve
2

2. About async and await

The async function returns a Promise object. When the function is executed, once it encounters await, it will return first ( hand over the thread, jump out of the async function body ), wait until the triggered asynchronous operation is completed, and then execute the following statements in the function body.

The statement after await will be executed immediately. When the promise is returned, because asynchronous operations are considered, and the next statement needs to know the result before it can be executed, the returned promise will be placed in the micro-queue after the subsequent synchronous statement is executed .

1.

async function async1(){
    
    
   console.log('async1 start');
   await async2();  会先执行async2函数,然后跳出async1,执行同步语句,然后将返回的promise放入微队列
   console.log('async1 end');
}
async function async2(){
    
    
   console.log('async');
}
console.log('script start');
setTimeout(function (){
    
    
   console.log('setTimeout');
},0);
async1();
new Promise(function(resolve){
    
    
   console.log('promise1');
   resolve();
}).then(function(){
    
    
   console.log('promise2');
});
console.log('script end');

script start
async1 start
async
promise1
script end
async1 end
promise2
setTimeout

2.

async function async1() {
    
    
    console.log('async1 start');
    const result = await async2();
    console.log(result);
    // 会先执行async2函数, 然后跳出async1, 同时将返回的promise放入微队列
    console.log('async1 end');
}
async function async2() {
    
    
    console.log('async');
    return "testAwait";
}
console.log('script start');
setTimeout(function() {
    
    
    console.log('setTimeout');
}, 0);
async1();
new Promise(function(resolve) {
    
    
    console.log('promise1');
    resolve();
}).then(function() {
    
    
    console.log('promise2');
});
new Promise(function(resolve) {
    
    
    console.log('promise3');
    resolve();
}).then(function() {
    
    
    console.log('promise4');
});
console.log('script end');

script start
async1 start
async
promise1
promise3
script end
promise2
promise4
testAwait
async1 end
setTimeout

Guess you like

Origin blog.csdn.net/wdhxs/article/details/110624773