第一题

setInterval每次执行的时候,都会把setInterval cb加入宏任务队列,每次事件环循环,在js执行栈的时候setInterval执行,将
setInterval cb放入宏任务队列
clearInterval(interval);清除宏任务队列里面的setInterval cb回调函数,并停止setInterval运行,每次事件环循环就不会setInterval执行了,就不会有新的setInterval cb放入宏任务队列了

  1. console.log('start');
  2. // setInterval1
  3. const interval = setInterval(() => {
  4. console.log('setInterval');
  5. }, 0)
  6. // setTimeout1
  7. setTimeout(() => {
  8. console.log('setTimeout 1')
  9. Promise.resolve()
  10. // promsie3
  11. .then(() => {
  12. console.log('promise 3')
  13. })
  14. // promise4
  15. .then(() => {
  16. console.log('promise 4')
  17. })
  18. // promise5
  19. .then(() => {
  20. // setTimeout2
  21. setTimeout(() => {
  22. console.log('setTimeout 2');
  23. Promise.resolve()
  24. // promise6
  25. .then(() => {
  26. console.log('promise 5');
  27. })
  28. // promise7
  29. .then(() => {
  30. console.log('promise 6');
  31. })
  32. // promise8
  33. .then(() => {
  34. clearInterval(interval);
  35. })
  36. })
  37. })
  38. })
  39. Promise.resolve()
  40. // promise1
  41. .then(() => {
  42. console.log('promise 1');
  43. })
  44. // promise2
  45. .then(() => {
  46. console.log('promise 2');
  47. })

image.png

image.png
image.png

第二道题

time 12m

  1. // setTimeout1
  2. setTimeout(() => {
  3. console.log('setTimeout 1');
  4. // setTimeout3
  5. setTimeout(() => {
  6. console.log('setTimeout 3');
  7. }, 1000);
  8. Promise.resolve()
  9. // promise4
  10. .then(data => {
  11. console.log('then3');
  12. });
  13. },0)
  14. Promise.resolve()
  15. // promise1
  16. .then(data => {
  17. console.log('then1');
  18. console.log('then2');
  19. Promise.resolve()
  20. // promise3
  21. .then(data1 => { console.log('then6') });
  22. });
  23. Promise.resolve()
  24. // promise2
  25. .then(data => {
  26. console.log('then4');
  27. console.log('then5');
  28. // setTimeout2
  29. setTimeout(() => {
  30. console.log('setTimeout 2');
  31. }, 1000);
  32. })
  33. console.log(2);

在js执行栈运行的promise,会把promise cb放入微任务队列中,js执行栈中promise1 cb与promise2 cb今日微任务栈,微任务执行时,从前到后运行,promise1 cb把promise3 cb放入js微任务栈
promise1 cb运行打印then1、then2,放入promise3-1 cb,promise3-1 cb,promise3-3 cb,之后promise2运行、
多个Promise.resolve() then cb是同时加进去的,同步代码

  1. // setTimeout1
  2. /*setTimeout(() => {
  3. console.log('setTimeout 1');
  4. // setTimeout3
  5. setTimeout(() => {
  6. console.log('setTimeout 3');
  7. }, 1000);
  8. Promise.resolve()
  9. // promise4
  10. .then(data => {
  11. console.log('then3');
  12. });
  13. }, 0)*/
  14. Promise.resolve()
  15. // promise1
  16. .then(data => {
  17. console.log('then1');
  18. console.log('then2');
  19. Promise.resolve()
  20. // promise3-1
  21. .then(data1 => {
  22. console.log('then6')
  23. });
  24. Promise.resolve()
  25. // promise3-2
  26. .then(data1 => {
  27. console.log('then6-2')
  28. });
  29. Promise.resolve()
  30. // promise3-3
  31. .then(data1 => {
  32. console.log('then6-3')
  33. });
  34. });
  35. Promise.resolve()
  36. // promise2
  37. .then(data => {
  38. console.log('then4');
  39. console.log('then5');
  40. // setTimeout2
  41. /* setTimeout(() => {
  42. console.log('setTimeout 2');
  43. }, 1000);*/
  44. })
  45. .then(data => {
  46. console.log('then4-2');
  47. console.log('then5-2');
  48. // setTimeout2
  49. /* setTimeout(() => {
  50. console.log('setTimeout 2');
  51. }, 1000);*/
  52. })
  53. .then(data => {
  54. console.log('then4-3');
  55. console.log('then5-3');
  56. })
  57. console.log(2);

image.png

then链式调用需要上一个then cb运行完成后才会加进微任务队列,下一个then cb加进去时,可能不会立即运行,因为它上面可能有别的微任务,当两个then回调时,是分别进行的,它们的结果会交叉显示

  1. Promise.resolve()
  2. // promise1
  3. .then(data => {
  4. console.log('then1');
  5. console.log('then2');
  6. Promise.resolve()
  7. // promise3
  8. .then(data1 => {
  9. console.log('then6')
  10. })
  11. .then(data => {
  12. console.log('then6-1')
  13. })
  14. .then(data => {
  15. console.log('then6-2')
  16. })
  17. /* Promise.resolve()
  18. // promise3
  19. .then(data1 => {
  20. console.log('then6-2')
  21. });
  22. Promise.resolve()
  23. // promise3
  24. .then(data1 => {
  25. console.log('then6-3')
  26. });*/
  27. });
  28. Promise.resolve()
  29. // promise2
  30. .then(data => {
  31. console.log('then4');
  32. console.log('then5');
  33. })
  34. .then(data => {
  35. console.log('then4-2');
  36. console.log('then5-2');
  37. })
  38. .then(data => {
  39. console.log('then4-3');
  40. console.log('then5-3');
  41. })
  42. console.log(2);

image.png

  1. // setTimeout1
  2. setTimeout(() => {
  3. console.log('setTimeout 1');
  4. // setTimeout3
  5. setTimeout(() => {
  6. console.log('setTimeout 3');
  7. }, 1000);
  8. Promise.resolve()
  9. // promise4
  10. .then(data => {
  11. console.log('then3');
  12. });
  13. }, 0)
  14. Promise.resolve()
  15. // promise1
  16. .then(data => {
  17. console.log('then1');
  18. console.log('then2');
  19. Promise.resolve()
  20. // promise3
  21. .then(data1 => {
  22. console.log('then6')
  23. });
  24. Promise.resolve()
  25. // promise3
  26. .then(data1 => {
  27. console.log('then6-2')
  28. });
  29. });
  30. Promise.resolve()
  31. // promise2
  32. .then(data => {
  33. console.log('then4');
  34. console.log('then5');
  35. // setTimeout2
  36. setTimeout(() => {
  37. console.log('setTimeout 2');
  38. }, 1000);
  39. })
  40. .then(data => {
  41. console.log('then4-2');
  42. console.log('then5-2');
  43. // setTimeout2
  44. setTimeout(() => {
  45. console.log('setTimeout 2');
  46. }, 1000);
  47. })
  48. console.log(2);

image.png

题目4

time 20m16s
image.png
image.png

题目5

time 23m06s