Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步编程: Promise 题目 1 - 4 #114

Open
leslie1943 opened this issue Jan 2, 2021 · 0 comments
Open

异步编程: Promise 题目 1 - 4 #114

leslie1943 opened this issue Jan 2, 2021 · 0 comments

Comments

@leslie1943
Copy link
Owner

Promise 题目 - 4

const promise1 = new Promise((resolve, reject) => {
  console.log("promise1");
  resolve("resolve1");
});
const promise2 = promise1.then((res) => {
  console.log(res);
});
console.log("1", promise1);
console.log("2", promise2);

// promise1
// 1, Promise{<resolved>, 'resolve1'}
// 2, Promise{<pending>}
// resolve1

过程分析

  1. 从上到下, 先遇到 Promise, 执行改构造函数中的代码 promise1
  2. 碰到 resolve 函数, 将 promise1 的状态改变为 resolved, 并将结果保存下来
  3. 碰到 promise1.then 这个微任务, 将它放入微任务队列
  4. promise2 是一个新的状态为 pendingPromise
  5. 执行同步代码1, 同时打印出 promise1 的状态是 resolved
  6. 执行同步代码2, 同时打印出 promise2 的状态是 pending
  7. 宏任务执行完毕, 查找微任务队列, 发现 promise1.then 这个微任务且状态为 resolved, 执行它
@leslie1943 leslie1943 changed the title 异步编程: Promise 题目 - 4 异步编程: Promise 题目 1 - 4 Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant