第一题

time 0

  1. await async2();
  2. console.log('async1 end');

await async2();返回promise,等待,相当于以下代码,把await之后的全部放then里面,多个await就是then链式调用

  1. async2().then(() => {console.log('async1 end')})
  1. async function async1() {
  2. console.log('async1 start');
  3. /**
  4. * Promise1
  5. * async2().then(() => {console.log('async1 end')})
  6. ***/
  7. await async2();
  8. console.log('async1 end');
  9. }
  10. async function async2() {
  11. console.log('async2');
  12. }
  13. console.log('script start');
  14. // setTimeout1
  15. setTimeout(function () {
  16. console.log('setTimeout');
  17. }, 0);
  18. async1();
  19. new Promise(function (resolve) {
  20. console.log('promise1');
  21. resolve();
  22. })
  23. // Promise2
  24. .then(function () {
  25. console.log('promise2');
  26. })
  27. console.log('script end');

image.png

第2题

time 6m46s
async2().then(()=>{console.log(‘async1 end’)});,先执行async2(),打印promise1,添加promise1.then cb,再执行.then,添加awaitPromise1 cb,这个其实不是then的链式调用

  1. async function async1() {
  2. console.log('async1 start');
  3. /**
  4. * awaitPromise1
  5. * async2().then(()=>{console.log('async1 end')});
  6. *
  7. ***/
  8. await async2();
  9. console.log('async1 end');
  10. }
  11. async function async2() {
  12. new Promise(function (resolve) {
  13. console.log('promise1');
  14. resolve();
  15. })
  16. // Promsie1
  17. .then(function () {
  18. console.log('promise2');
  19. });
  20. }
  21. console.log('script start');
  22. // setTimeout1
  23. setTimeout(function () {
  24. console.log('setTimeout');
  25. }, 0)
  26. async1();
  27. new Promise(function (resolve) {
  28. console.log('promise3');
  29. resolve();
  30. })
  31. // Promise2
  32. .then(function () {
  33. console.log('promise4');
  34. });
  35. console.log('script end');

执行栈
script start
async1()->async1 start
async2()->promise1
promise3
script end
Promsie1.then cb 2th->promise2
Promsie1.then cb 2th->async1 end
Promise2.then cb 4th->promise4
setTimeout1 cb 1th->setTimeout

微任务
Promsie1
Promsie1.then cb 2th x
awaitPromise1
awaitPromise1.then cb 3th x
Promise2
Promise2.then cb 4th
宏任务
setTimeout1
setTimeout1 cb 1th

  1. async function async1() {
  2. console.log('async1 start');
  3. /**
  4. * awaitPromise1
  5. * async2().then(()=>{console.log('async1 end')});
  6. *
  7. ***/
  8. await async2();
  9. console.log('async1 end');
  10. }
  11. async function async2() {
  12. new Promise(function (resolve) {
  13. console.log('promise1');
  14. resolve();
  15. })
  16. // Promsie1
  17. .then(function () {
  18. console.log('promise2');
  19. })
  20. // Promsie1-2
  21. .then(function () {
  22. console.log('promise2-2');
  23. });
  24. }
  25. console.log('script start');
  26. // setTimeout1
  27. setTimeout(function () {
  28. console.log('setTimeout');
  29. }, 0)
  30. async1();
  31. new Promise(function (resolve) {
  32. console.log('promise3');
  33. resolve();
  34. })
  35. // Promise2
  36. .then(function () {
  37. console.log('promise4');
  38. })
  39. .then(function () {
  40. console.log('promise4-2');
  41. });
  42. console.log('script end');

image.png
证明了 async2().then(()=>{console.log(‘async1 end’)});不是链式调用,因为在Promsie1-2 cb之前awaitPromise1就运行了

第三题

time 12m12s
image.png
image.png

第3题

time 19m18s

  1. var promise = new Promise((resolve) => {
  2. console.log(1);
  3. resolve();
  4. });
  5. // setTimeout1
  6. setTimeout(() => {
  7. console.log(2);
  8. });
  9. // Promise1
  10. promise.then(() => {
  11. console.log(3);
  12. });
  13. var promise2 = getPromise();
  14. async function getPromise() {
  15. console.log(5);
  16. /**
  17. * awaitPromise
  18. * 因为await promise本身就是promise
  19. * promise.then(()=>{console.log(6)})
  20. **/
  21. await promise;
  22. console.log(6);
  23. }
  24. console.log(8);

image.png

image.png

第4题

time 25m25s
多次调用eat方法

  1. const LazyMan = function (name) {
  2. console.log(`Hi i am ${name}`);
  3. function _eat(food) {
  4. console.log(`I am eating ${food}`);
  5. }
  6. const callbacks = [];
  7. class F {
  8. sleep(timeout) {
  9. setTimeout(function () {
  10. console.log(`等待了${timeout}秒....`);
  11. console.log(callbacks);
  12. callbacks.forEach(cb => cb())
  13. }, timeout);
  14. return this;
  15. };
  16. eat(food) {
  17. callbacks.push(_eat.bind(null, food));
  18. return this;
  19. }
  20. }
  21. return new F();
  22. };
  23. LazyMan('Tony').sleep(10).eat('lunch').eat('meal');

image.png

image.png