promise示例

  1. new Promise(resolve => {
  2. console.log('promise1')
  3. resolve()
  4. }).then(() => {
  5. console.log('then11')
  6. new Promise(resolve => {
  7. console.log('promise2')
  8. resolve()
  9. }).then(() => {
  10. console.log('then21')
  11. }).then(() => {
  12. console.log('then22')
  13. })
  14. }).then(() => {
  15. console.log('then12')
  16. })

过程分析:
1、同步代码执行过程中,外层promise实例的exector执行,并且同步修改其状态结果,另外通知then方法存放的方法执行,但是此时then还没有执行到,里面没有存放方法,所以只是修改状态结果
2、exector函数执行完成后,将then里面的方法存起来。此时promise实例1的状态已经是成功的了,所以微任务1是当同步代码执行完成后立即执行,而微任务2要等到微任务1中的代码执行完成后才会达到可执行状态。
同步代码执行完成后,此时任务队列中已经存放了微任务1和微任务2两个,微任务1的状态时可执行的。
3、执行微任务1,其中内层promise的exector同步执行,并且实例状态成功,存放微任务3[存放的时候状态应是可执行的,所以比微任务2先达到可执行状态]和微任务4[需要等到微任务3执行成功后才会执行]
微任务1的同步代码执行完成后,任务队列中的2和3都已经到了可执行状态,但是3比2要早达到,先执行3
4、执行微任务3完成后,微任务4达到可执行状态
5、执行微任务2
6、执行微任务4
如果是使用return new Promise的话,那么微任务2需要等到微任务4执行完成才达到可执行条件
1.png


  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');

1-0.png


  1. let body = document.body;
  2. body.addEventListener('click', function () {
  3. Promise.resolve().then(() => {
  4. console.log(1);
  5. });
  6. console.log(2);
  7. });
  8. body.addEventListener('click', function () {
  9. Promise.resolve().then(() => {
  10. console.log(3);
  11. });
  12. console.log(4);
  13. });

宏任务1执行的console的时候微任务1已经达到了可执行的状态了,所以先执行微任务1再执行宏任务2
Promise的示例 - 图3


  1. console.log('start');
  2. let intervalId;
  3. Promise.resolve().then(() => {
  4. console.log('p1');
  5. }).then(() => {
  6. console.log('p2');
  7. });
  8. setTimeout(() => {
  9. Promise.resolve().then(() => {
  10. console.log('p3');
  11. }).then(() => {
  12. console.log('p4');
  13. });
  14. intervalId = setInterval(() => {
  15. console.log('interval');
  16. }, 3000);
  17. console.log('timeout1');
  18. }, 0);

1-3.png


  1. setTimeout(() => {
  2. console.log('a');
  3. });
  4. Promise.resolve().then(() => {
  5. console.log('b');
  6. }).then(() => {
  7. return Promise.resolve('c').then(data => {
  8. setTimeout(() => {
  9. console.log('d')
  10. });
  11. console.log('f');
  12. return data;
  13. });
  14. }).then(data => {
  15. console.log(data);
  16. });

1-4.png


  1. function func1() {
  2. console.log('func1 start');
  3. return new Promise(resolve => {
  4. resolve('OK');
  5. });
  6. }
  7. function func2() {
  8. console.log('func2 start');
  9. return new Promise(resolve => {
  10. setTimeout(() => {
  11. resolve('OK');
  12. }, 10);
  13. });
  14. }
  15. console.log(1);
  16. setTimeout(async () => {
  17. console.log(2);
  18. await func1();
  19. console.log(3);
  20. }, 20);
  21. for (let i = 0; i < 90000000; i++) {}
  22. //循环大约要进行80MS左右
  23. console.log(4);
  24. func1().then(result => {
  25. console.log(5);
  26. });
  27. func2().then(result => {
  28. console.log(6);
  29. });
  30. setTimeout(() => {
  31. console.log(7);
  32. }, 0);
  33. console.log(8);

1-5.png


async/await的示例

  1. var resolveAfter2Seconds = function resolveAfter2Seconds() {
  2. console.log("starting slow promise");
  3. return new Promise(resolve => {
  4. setTimeout(function () {
  5. resolve("slow");
  6. console.log("slow promise is done");
  7. }, 2000);
  8. });
  9. };
  10. var resolveAfter1Second = function resolveAfter1Second() {
  11. console.log("starting fast promise");
  12. return new Promise(resolve => {
  13. setTimeout(function () {
  14. resolve("fast");
  15. console.log("fast promise is done");
  16. }, 1000);
  17. });
  18. };
  19. // sequential:相继的 /[sɪˈkwenʃl]/
  20. var sequential = async function sequential() {
  21. console.log('==SEQUENTIAL START==');
  22. const slow = await resolveAfter2Seconds();
  23. console.log(slow);
  24. const fast = await resolveAfter1Second();
  25. console.log(fast);
  26. };
  27. // concurrent:同时发生的 /[kənˈkʌrənt]/
  28. var concurrent = async function concurrent() {
  29. console.log('==CONCURRENT START with await==');
  30. const slow = resolveAfter2Seconds();
  31. const fast = resolveAfter1Second();
  32. console.log(await slow);
  33. console.log(await fast);
  34. };
  35. var concurrentPromise = function concurrentPromise() {
  36. console.log('==CONCURRENT START with Promise.all==');
  37. return Promise.all([resolveAfter2Seconds(), resolveAfter1Second()])
  38. .then((messages) => {
  39. console.log(messages[0]);
  40. console.log(messages[1]);
  41. });
  42. };
  43. // parallel:平行的 /[ˈpærəlel]/
  44. var parallel = async function parallel() {
  45. console.log('==PARALLEL with await Promise.all==');
  46. await Promise.all([
  47. (async () => {
  48. let result = await resolveAfter2Seconds();
  49. console.log(result);
  50. })(),
  51. (async () => {
  52. let result = await resolveAfter1Second();
  53. console.log(result);
  54. })(),
  55. ]);
  56. };
  57. var parallelPromise = function parallelPromise() {
  58. console.log('==PARALLEL with Promise.then==');
  59. resolveAfter2Seconds().then((message) => console.log(message));
  60. resolveAfter1Second().then((message) => console.log(message));
  61. };
  1. // 相继
  2. sequential()

2.png


  1. // 同时发生的
  2. concurrent()

3.png


  1. concurrentPromise()

4.png


  1. // 平行的
  2. parallel()

5.png