跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[问与答] 一个 Promise/async/await 的执行顺序问题求助🙏

发表于
async function async1() {
  async2().then(() => {
    console.log('async1 end');
  });
}
async function async2() {
  return new Promise((resolve, reject) => {
    resolve();
  })
}

async1();
new Promise(function(resolve) {
  resolve();
}).then(function() {
  console.log('promise2');
})

输出结果为

promise2
async1 end

(面试时候遇到的,去掉了无关的 log)

想知道为什么 promise2 早于 async1 end ?

我的想法是 async2() 的 then 方法早于 new Promise 的 then 方法执行,那么其回调函数就应该先被放入微任务队列,也就应该先被执行,但实际情况不是这样。

另外,如果我把 async1async2async 关键字去掉,那么输出就变为

async1 end
promise2

求大佬解答

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.