事件环

题目1

  • 什么是宏任务?
  • 什么是微任务?
  • 同步任务?
  • 异步任务?
  • 事件轮询是什么?

my answer:

宏任务:定时器

微任务:resolve/reject的回调函数then()

同步任务:直接从调用栈中先入后出执行的程序

异步任务:如定时器是异步任务,存在阻塞问题

事件轮询:不太明确答案

解答:

  • 宏任务:本身并不明确,如果当前宏是明确,当前宏就是执行栈中的执行的任务(所有或单一),其他说法实际上指的是异步任务,
  • 微任务:promise/then,属于异步任务,针对宏任务来说
  • 同步任务:
  • 异步任务:ajax/then
  • 事件轮询:事件回调通过异步任务出现不断循环,JavaScript先执行先同步,看事件回调的任务,可能是微任务,存在优先队列的问题,后异步,反复执行

补充:队列的实现?

题目2

  1. //1.
  2. Promise.resolve(2)
  3. .then(3) //并不是函数
  4. .then(Promise.resolve(4)) //这里是一个对象本身
  5. .then(console.log) //console.log 是一个函数
  6. //答案:2 所以直接返回函数的结果
  7. //为什么?
  8. 只有是函数才会注册到成功/失败队列中,当条件完成以后,执行函数
  1. //2.
  2. let p1 = new Promise(function(resolve, reject) {
  3. reject(42);
  4. });
  5. p1.catch(function(value) {
  6. console.log(value); //42
  7. return value + 1;
  8. }).then(function(value) {
  9. console.log(value); //43
  10. });
  11. //答案: 42 43
  1. //3.
  2. let p1 = new Promise(function(resolve, reject) {
  3. resolve(42);
  4. });
  5. let p2 = new Promise(function(resolve, reject) {
  6. reject(43); //这里报错,没有捕获到
  7. });
  8. var p3 = p1.then(function(value) {
  9. console.log(value); //42
  10. return p2;
  11. })
  12. .then(function(value) {
  13. console.log(value); //42
  14. });
  15. //答案:42 undefined(因为这里p.then里没有return ) 报错
  16. //为什么?
  17. 42 p1 成功的回调
  18. 报错:在reject的时候直接报错(没有注册失败的回调),
  1. //3.改写
  2. let p1 = new Promise(function(resolve, reject) {
  3. resolve(42);
  4. });
  5. var p3 = p1.then(function(value) {
  6. console.log(value); //42
  7. return new Promise(function(resolve, reject) {
  8. reject(43); //这里报错,没有捕获到
  9. });
  10. })
  11. .then(function(value) {
  12. console.log(value); //42
  13. })
  14. //应该有catch
  15. .catch(function(err){
  16. console.log(err);
  17. })
  1. //4.
  2. 同步任务执行
  3. //挂起
  4. setTimeout(() => {
  5. console.log("1");
  6. Promise.resolve().then(() => {
  7. console.log("2");
  8. });
  9. });
  10. Promise.resolve().then(() => {
  11. console.log("3");
  12. //挂起
  13. setTimeout(() => {
  14. console.log("4");
  15. });
  16. });
  17. //答案:
  18. 3 1 2 4
  19. 先执行微任务(promise/then)
  20. 再执行宏任务(定时器)
  1. //5.
  2. Promise.resolve()
  3. .then(() => {
  4. return Promise.resolve() // 在这加return 和不加return 的区别;
  5. .then(() => {
  6. console.log(1);
  7. })
  8. .then(() => {
  9. console.log(2);
  10. });
  11. })
  12. .then(val => {
  13. console.log(3);
  14. })
  15. .then(() => {
  16. console.log(4);
  17. })
  18. .then(() => {
  19. console.log(5);
  20. });
  21. //答案:
  22. return 12345
  23. 没有return 13245
  1. //6.
  2. async function async1() {
  3. await async2();
  4. console.log("1");
  5. }
  6. async function async2() {
  7. console.log("2");
  8. }
  9. async1();
  10. console.log(10);
  11. 答案:2 10 1
  12. 2
  13. Promise{pending}
  14. 10
  15. 1
  1. //7.
  2. async function async1() {
  3. await async2();
  4. console.log("2");
  5. }
  6. async function async2() {
  7. Promise.resolve(1)
  8. .then(() => {
  9. console.log(3);
  10. })
  11. .then(() => {
  12. console.log(4);
  13. })
  14. .then(() => {
  15. console.log(11);
  16. });
  17. }
  18. async1();
  19. setTimeout(function () {
  20. console.log(5);
  21. });
  22. new Promise(resolve => {
  23. console.log(6);
  24. resolve();
  25. })
  26. .then(() => {
  27. console.log(7);
  28. })
  29. .then(() => {
  30. console.log(8);
  31. })
  32. .then(() => {
  33. console.log(9);
  34. });
  35. console.log(10);