1

  1. console.log('script start');
  2. setTimeout(function () {
  3. console.log('setTimeout');
  4. }, 0);
  5. new Promise(resolve => {
  6. console.log('Promise');
  7. resolve();
  8. }).then(function () {
  9. setTimeout(function () {
  10. console.log('setTimeout in promise1');
  11. }, 0);
  12. console.log('promise1');
  13. }).then(function () {
  14. console.log('promise2');
  15. });
  16. console.log('script end');
  17. // script start
  18. // Promise
  19. // script end
  20. // promise1
  21. // promise2
  22. // setTimeout
  23. // setTimeout in promise1

2

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

特别注意**async2**执行的顺序, await的函数内部 其实是同步执行的,相当于在promise里面。

  1. async function async1() {
  2. console.log('async1 start');
  3. await async2();
  4. console.log('async1 end');
  5. }
  6. / ==
  7. async function async1() {
  8. console.log('async1 start');
  9. Promise.resolve(async2()).then(() => {
  10. console.log('async1 end');
  11. })
  12. }

3

  1. async function async1() {
  2. console.log("async1 start");
  3. await async2();
  4. setTimeout(function () {
  5. console.log("setTimeout1");
  6. }, 0);
  7. }
  8. async function async2() {
  9. setTimeout(function () {
  10. console.log("setTimeout2");
  11. }, 0);
  12. }
  13. console.log("script start");
  14. setTimeout(function () {
  15. console.log("setTimeout3");
  16. }, 0);
  17. async1();
  18. new Promise(function (resolve) {
  19. console.log("promise1");
  20. resolve();
  21. }).then(function () {
  22. console.log("promise2");
  23. });
  24. console.log("script end");
  25. // script start
  26. // async1 start
  27. // promise1
  28. // script end
  29. // promise2
  30. // setTimeout3
  31. // setTimeout2
  32. // setTimeout1

4

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

5

  1. console.log("start");
  2. setTimeout(() => {
  3. console.log("children2");
  4. Promise.resolve().then(() => {
  5. console.log("children3");
  6. });
  7. }, 0);
  8. new Promise((resolve, reject) => {
  9. console.log("children4");
  10. setTimeout(() => {
  11. console.log("children5");
  12. resolve("children6");
  13. }, 0);
  14. }).then((res) => {
  15. console.log("children7");
  16. setTimeout(() => {
  17. console.log(res);
  18. }, 0);
  19. });
  20. //start
  21. //children4
  22. //childern2
  23. //childern3
  24. //childern5
  25. //childern7
  26. //children6

6

  1. const p = () => {
  2. return new Promise((resolve, reject) => {
  3. const p1 = new Promise((resolve, reject) => {
  4. setTimeout(() => {
  5. resolve(1);
  6. }, 0);
  7. // resolve(2);
  8. });
  9. p1.then((res) => {
  10. console.log(res);
  11. });
  12. console.log(3);
  13. resolve(4);
  14. });
  15. };
  16. p().then((res) => {
  17. console.log(res);
  18. });
  19. console.log("end");
  20. // 3
  21. // end
  22. // 4
  23. // 1