题目3

  1. let res = function () {
  2. console.log(1);
  3. return new Promise((resolve, reject) => {
  4. console.log(2);
  5. resolve(4);
  6. })
  7. }
  8. new Promise(async (resolve, reject) => {
  9. console.log(3);
  10. let test = await res();
  11. console.log(test);
  12. })
  13. console.log(5);
  14. new Promise((resolve, reject) => {
  15. console.log(6);
  16. });
  17. console.log(7);

分析

time 2m

  1. let res = function () {
  2. console.log(1);
  3. return new Promise((resolve, reject) => {
  4. console.log(2);
  5. resolve(4);
  6. })
  7. }
  8. new Promise(async (resolve, reject) => {
  9. console.log(3);
  10. //promise1
  11. let test = await res();
  12. /**
  13. * await res()
  14. *相当于res()运行,之后then
  15. * res().then((res)=>{
  16. * console.log(test);
  17. * })
  18. */
  19. console.log(test);
  20. })
  21. console.log(5);
  22. new Promise((resolve, reject) => {
  23. console.log(6);
  24. });
  25. console.log(7);
  1. async function test(){
  2. }
  3. console.log(test())

test()运行返回promise
image.png

async function test(){}相当于new Promise()

await test()相相当于test().then,test()返回promise,相当于promise.then(),类似于下面代码

  1. let test = await res();
  2. /**
  3. * await res()
  4. *相当于res()运行,之后then
  5. * res().then((res)=>{
  6. * console.log(test);
  7. * })
  8. */

可以分析成、等价于以下代码

  1. ;(function () {
  2. console.log(1);
  3. return new Promise((resolve, reject) => {
  4. console.log(2);
  5. resolve(4);
  6. })
  7. })().then();
  8. new Promise((resolve, reject) => {
  9. console.log(2);
  10. resolve(4);
  11. }).then()

执行栈
script
function ->res 这只是res赋值为函数,但没有运行
3 new promise回调函数虽然是async,但是里面是同步代码,await有区别
1 运行res(),打印1
2
5
6
7
promise1.then cb ->4

宏任务

微任务
promise1
promise1.then cb x

变形1

time 8m53s

  1. let res = function () {
  2. console.log(1);
  3. return new Promise((resolve, reject) => {
  4. setTimeout(() => {
  5. new Promise((resolve) => {
  6. console.log(2);
  7. setTimeout(() => {
  8. console.log(3)
  9. })
  10. })
  11. }, 0)
  12. resolve(5);
  13. })
  14. }
  15. new Promise(async (resolve, reject) => {
  16. setTimeout(() => {
  17. Promise.resolve().then(() => {
  18. console.log(4);
  19. })
  20. }, 0);
  21. //promise1
  22. let test = await res();
  23. /**
  24. * await res()
  25. *
  26. * res().then((res)=>{
  27. * console.log(test);
  28. * })
  29. */
  30. console.log(test);
  31. })
  32. setTimeout(() => {
  33. console.log(6);
  34. }, 0)
  35. new Promise((resolve, reject) => {
  36. setTimeout(() => {
  37. console.log(7);
  38. })
  39. }, 0);
  40. console.log(8);

image.png

分析

先标注上setTimeout、promise,可以不标序号,分析时再标记,现在标记,省得分析时候忘记了

  1. let res = function () {
  2. console.log(1);
  3. return new Promise((resolve, reject) => {
  4. //setTimeout
  5. setTimeout(() => {
  6. new Promise((resolve) => {
  7. console.log(2);
  8. //setTimeout
  9. setTimeout(() => {
  10. console.log(3)
  11. })
  12. })
  13. }, 0)
  14. resolve(5);
  15. })
  16. }
  17. new Promise(async (resolve, reject) => {
  18. //setTimeout
  19. setTimeout(() => {
  20. //promise
  21. Promise.resolve().then(() => {
  22. console.log(4);
  23. })
  24. }, 0);
  25. //promise
  26. let test = await res();
  27. console.log(test);
  28. })
  29. //setTimeout
  30. setTimeout(() => {
  31. console.log(6);
  32. }, 0)
  33. new Promise((resolve, reject) => {
  34. //setTimeout
  35. setTimeout(() => {
  36. console.log(7);
  37. })
  38. }, 0);
  39. console.log(8);

在setTimeout后面标注序号,省得之后忘记了
image.png

image.png
写awaitPromise与promise都可以
awaitPromise.then cb与promise.then都可以,做好加数字序号,可能太麻烦了
这道题与上面的题目意思差不多,但步骤很多,步骤多就容易犯错、失误,得精神集中

执行栈

微任务

宏任务

image.png