main.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Светофор + domEventPromise
  2. // function delay(ms) {
  3. // return new Promise(ok => setTimeout(() => ok(ms), ms)).then(response => console.log(response));
  4. // }
  5. //
  6. // let div = document.createElement('div');
  7. // let div1 = document.createElement('div');
  8. // let div2 = document.createElement('div');
  9. //
  10. // div.className = 'trafficLight';
  11. // div1.className = 'trafficLight';
  12. // div2.className = 'trafficLight';
  13. //
  14. // placeForTrafficLight.appendChild(div);
  15. // placeForTrafficLight.appendChild(div1);
  16. // placeForTrafficLight.appendChild(div2);
  17. //
  18. // let promise = async function trafficLight() {
  19. //
  20. // while (true) {
  21. // div.style.background = 'green';
  22. // div1.style.background = 'white';
  23. // div2.style.background = 'white';
  24. // await delay(4000);
  25. // div.style.background = 'white';
  26. // div1.style.background = 'yellow';
  27. // div2.style.background = 'white';
  28. // await delay(2000);
  29. // div.style.background = 'white';
  30. // div1.style.background = 'white';
  31. // div2.style.background = 'red';
  32. // await delay(30000);
  33. // }
  34. // };
  35. //
  36. // promise().then(response => console.log(response));
  37. //
  38. // function domEventPromise(button, eventName) {
  39. // return new Promise((resolve) => {
  40. // button.addEventListener(eventName, (event) => {
  41. // resolve(event);
  42. // button.disabled = true;
  43. // });
  44. //
  45. // button.removeEventListener(eventName, (event) => {
  46. // resolve(event.delay(4000));
  47. // });
  48. // });
  49. // }
  50. //
  51. // let button = document.createElement('button');
  52. // button.className = 'buttonTraficLight';
  53. // button.textContent = 'Push';
  54. // placeForButton.appendChild(button);
  55. //
  56. // const pushPromise = domEventPromise(button, 'click');
  57. // pushPromise.then(e => console.log('event click happens', e));
  58. // PedestrianTrafficLight
  59. function delay(ms) {
  60. return new Promise(ok => setTimeout(() => ok(ms), ms)).then(response => console.log(response));
  61. }
  62. let div = document.createElement('div');
  63. let div1 = document.createElement('div');
  64. let div2 = document.createElement('div');
  65. div.className = 'trafficLight';
  66. div1.className = 'trafficLight';
  67. div2.className = 'trafficLight';
  68. placeForTrafficLight.appendChild(div);
  69. placeForTrafficLight.appendChild(div1);
  70. placeForTrafficLight.appendChild(div2);
  71. let button = document.createElement('button');
  72. button.className = 'buttonTraficLight';
  73. button.textContent = 'Push';
  74. placeForButton.appendChild(button);
  75. async function pedestrianTrafficLight() {
  76. while (true) {
  77. switch ('red') {
  78. case 'red':
  79. div.style.background = 'red';
  80. div1.style.background = 'white';
  81. div2.style.background = 'white';
  82. button.disabled = true;
  83. await delay(4000);
  84. button.disabled = false;
  85. await Promise.race([delay(10000), domEventPromise(button, 'click').then(e => console.log('event click happens', e))]);
  86. }
  87. switch ('yellow') {
  88. case 'yellow':
  89. div.style.background = 'white';
  90. div1.style.background = 'yellow';
  91. div2.style.background = 'white';
  92. await Promise.race([delay(1000), domEventPromise(button, 'click').then(e => console.log('event click happens', e))]);
  93. }
  94. switch ('green') {
  95. case 'green':
  96. button.disabled = true;
  97. div.style.background = 'white';
  98. div1.style.background = 'white';
  99. div2.style.background = 'green';
  100. await delay(3000);
  101. }
  102. }
  103. }
  104. pedestrianTrafficLight();
  105. function domEventPromise(button, eventName) {
  106. return new Promise((resolve, reject) => {
  107. button.addEventListener(eventName, (event) => {
  108. resolve(event);
  109. });
  110. button.removeEventListener(eventName, (event) => {
  111. resolve(event);
  112. });
  113. });
  114. }
  115. function delay(ms) {
  116. return new Promise(ok => setTimeout(() => ok(ms), ms)).then(response => console.log(response));
  117. }
  118. async function speedtest(getPromise, count, parallel = 1) {
  119. let counter = 0;
  120. let paralCount = 0;
  121. let arrPromises = [];
  122. const start = new Date().getTime();
  123. while (counter !== count) {
  124. counter++;
  125. while (paralCount !== parallel) {
  126. paralCount++;
  127. arrPromises.push(await getPromise());
  128. }
  129. Promise.all([arrPromises]).then(result => console.log(result));
  130. }
  131. const end = new Date().getTime();
  132. let duration = end - start;
  133. let parallelDuration = duration / (count * parallel);
  134. let parallelSpeed = parallel / parallelDuration;
  135. let queryDuration = duration / count;
  136. let querySpeed = count / queryDuration;
  137. return {
  138. duration,
  139. querySpeed,
  140. queryDuration,
  141. parallelSpeed,
  142. parallelDuration
  143. };
  144. }
  145. speedtest(() => delay(1000), 10, 10).then(result => console.log(result));
  146. // // speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5);