js event loop

139 阅读1分钟

能正确写出这道题的执行顺序的人说明对js异步任务的理解已经够用了

async function foo() {
  console.log("13");
  await bar();
  console.log("15");
}

function bar() {
  console.log("14");
}

setTimeout(function() {
  console.log("2");
  new Promise(function(resolve) {
     console.log("4");
     resolve();
  }).then(function() {
     console.log("5");
  });
});
new Promise(function(resolve) {
   console.log("7");
   resolve();
}).then(function() {
   console.log("8");
});

setTimeout(function() {
  console.log("9");

  new Promise(function(resolve) {
      console.log("11");
      resolve();
  }).then(function() {
      console.log("12");
  });
});

foo();