Guest User

Untitled

a guest
Jul 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. var keepsHisWord;
  2. keepsHisWord = true;
  3. promise1 = new Promise(function(resolve, reject) {
  4. if (keepsHisWord) {
  5. resolve("The man likes to keep his word");
  6. } else {
  7. reject("The man doesnt want to keep his word");
  8. }
  9. });
  10. console.log(promise1);
  11.  
  12. promise2 = new Promise(function(resolve, reject) {
  13. setTimeout(function() {
  14. resolve({
  15. message: "The man likes to keep his word",
  16. code: "aManKeepsHisWord"
  17. });
  18. }, 10 * 1000);
  19. });
  20. console.log(promise2);
  21.  
  22. keepsHisWord = false;
  23. promise3 = new Promise(function(resolve, reject) {
  24. if (keepsHisWord) {
  25. resolve("The man likes to keep his word");
  26. } else {
  27. reject("The man doesn't want to keep his word");
  28. }
  29. });
  30. console.log(promise3);
  31.  
  32. var momsPromise = new Promise(function(resolve, reject) {
  33. momsSavings = 20000;
  34. priceOfPhone = 60000;
  35. if (momsSavings > priceOfPhone) {
  36. resolve({
  37. brand: "iphone",
  38. model: "6s"
  39. });
  40. } else {
  41. reject("We donot have enough savings. Let us save some more money.");
  42. }
  43. });
  44. momsPromise.then(function(value) {
  45. console.log("Hurray I got this phone as a gift ", JSON.stringify(value));
  46. });
  47. momsPromise.catch(function(reason) {
  48. console.log("Mom coudn't buy me the phone because ", reason);
  49. });
  50. momsPromise.finally(function() {
  51. console.log(
  52. "Irrespecitve of whether my mom can buy me a phone or not, I still love her"
  53. );
  54. });
  55.  
  56. momsPromise.then(
  57. function(value) {
  58. console.log("Hurray I got this phone as a gift ", JSON.stringify(value));
  59. },
  60. function(reason) {
  61. console.log("Mom coudn't buy me the phone because ", reason);
  62. }
  63. );
  64.  
  65. function getRandomNumber(start = 1, end = 10) {
  66. //works when both start,end are >=1 and end > start
  67. return parseInt(Math.random() * end) % (end-start+1) + start;
  68. }
  69.  
  70. function getRandomNumber(start = 1, end = 10) {
  71. //works when both start and end are >=1
  72. return (parseInt(Math.random() * end) % (end - start + 1)) + start;
  73. }
  74. var promiseTRRARNOSG = (promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator = function() {
  75. return new Promise(function(resolve, reject) {
  76. let randomNumberOfSeconds = getRandomNumber(2, 10);
  77. setTimeout(function() {
  78. let randomiseResolving = getRandomNumber(1, 10);
  79. if (randomiseResolving > 5) {
  80. resolve({
  81. randomNumberOfSeconds: randomNumberOfSeconds,
  82. randomiseResolving: randomiseResolving
  83. });
  84. } else {
  85. reject({
  86. randomNumberOfSeconds: randomNumberOfSeconds,
  87. randomiseResolving: randomiseResolving
  88. });
  89. }
  90. }, randomNumberOfSeconds * 1000);
  91. });
  92. });
  93. var testProimse = promiseTRRARNOSG();
  94. testProimse.then(function(value) {
  95. console.log("Value when promise is resolved : ", value);
  96. });
  97. testProimse.catch(function(reason) {
  98. console.log("Reason when promise is rejected : ", reason);
  99. });
  100. // Let us loop through and create ten different promises using the function to see some variation. Some will be resolved and some will be rejected.
  101. for (i=1; i<=10; i++) {
  102. let promise = promiseTRRARNOSG();
  103. promise.then(function(value) {
  104. console.log("Value when promise is resolved : ", value);
  105. });
  106. promise.catch(function(reason) {
  107. console.log("Reason when promise is rejected : ", reason);
  108. });
  109. }
  110.  
  111. var promise3 = Promise.reject("Not interested");
  112. promise3.then(function(value){
  113. console.log("This will not run as it is a resolved promise. The resolved value is ", value);
  114. });
  115. promise3.catch(function(reason){
  116. console.log("This run as it is a rejected promise. The reason is ", reason);
  117. });
  118.  
  119. var promise4 = Promise.resolve(1);
  120. promise4.then(function(value){
  121. console.log("This will run as it is a resovled promise. The resolved value is ", value);
  122. });
  123. promise4.catch(function(reason){
  124. console.log("This will not run as it is a resolved promise", reason);
  125. });
  126.  
  127.  
  128. var promise4 = Promise.resolve(1);
  129. promise4.then(function(value){
  130. console.log("This will run as it is a resovled promise. The resolved value is ", value);
  131. });
  132. promise4.then(function(value){
  133. console.log("This will also run as multiple handlers can be added. Printing twice the resolved value which is ", value * 2);
  134. });
  135. promise4.catch(function(reason){
  136. console.log("This will not run as it is a resolved promise", reason);
  137. });
  138.  
  139.  
  140. var promiseTRSANSG = (promiseThatResolvesAfterNSecondsGenerator = function(
  141. n = 0
  142. ) {
  143. return new Promise(function(resolve, reject) {
  144. setTimeout(function() {
  145. resolve({
  146. resolvedAfterNSeconds: n
  147. });
  148. }, n * 1000);
  149. });
  150. });
  151. var promiseTRJANSG = (promiseThatRejectsAfterNSecondsGenerator = function(
  152. n = 0
  153. ) {
  154. return new Promise(function(resolve, reject) {
  155. setTimeout(function() {
  156. reject({
  157. rejectedAfterNSeconds: n
  158. });
  159. }, n * 1000);
  160. });
  161. });
  162.  
  163. console.time("Promise.All");
  164. var promisesArray = [];
  165. promisesArray.push(promiseTRSANSG(1));
  166. promisesArray.push(promiseTRSANSG(4));
  167. promisesArray.push(promiseTRSANSG(2));
  168. var handleAllPromises = Promise.all(promisesArray);
  169. handleAllPromises.then(function(values) {
  170. console.timeEnd("Promise.All");
  171. console.log("All the promises are resolved", values);
  172. });
  173. handleAllPromises.catch(function(reason) {
  174. console.log("One of the promises failed with the following reason", reason);
  175. });
  176.  
  177. console.time("Promise.All");
  178. var promisesArray = [];
  179. promisesArray.push(1);
  180. promisesArray.push(4);
  181. promisesArray.push(2);
  182. var handleAllPromises = Promise.all(promisesArray);
  183. handleAllPromises.then(function(values) {
  184. console.timeEnd("Promise.All");
  185. console.log("All the promises are resolved", values);
  186. });
  187. handleAllPromises.catch(function(reason) {
  188. console.log("One of the promises failed with the following reason", reason);
  189. });
  190.  
  191.  
  192. console.time("Promise.All");
  193. var promisesArray = [];
  194. promisesArray.push(promiseTRSANSG(1));
  195. promisesArray.push(promiseTRSANSG(5));
  196. promisesArray.push(promiseTRSANSG(3));
  197. promisesArray.push(promiseTRJANSG(2));
  198. promisesArray.push(promiseTRSANSG(4));
  199. var handleAllPromises = Promise.all(promisesArray);
  200. handleAllPromises.then(function(values) {
  201. console.timeEnd("Promise.All");
  202. console.log("All the promises are resolved", values);
  203. });
  204. handleAllPromises.catch(function(reason) {
  205. console.timeEnd("Promise.All");
  206. console.log("One of the promises failed with the following reason ", reason);
  207. });
  208.  
  209.  
  210. console.time("Promise.race");
  211. var promisesArray = [];
  212. promisesArray.push(promiseTRSANSG(4));
  213. promisesArray.push(promiseTRSANSG(3));
  214. promisesArray.push(promiseTRSANSG(2));
  215. promisesArray.push(promiseTRJANSG(3));
  216. promisesArray.push(promiseTRSANSG(4));
  217. var promisesRace = Promise.race(promisesArray);
  218. promisesRace.then(function(values) {
  219. console.timeEnd("Promise.race");
  220. console.log("The fasted promise resolved", values);
  221. });
  222. promisesRace.catch(function(reason) {
  223. console.timeEnd("Promise.race");
  224. console.log("The fastest promise rejected with the following reason ", reason);
  225. });
  226.  
  227.  
  228. console.time("Promise.race");
  229. var promisesArray = [];
  230. promisesArray.push(promiseTRSANSG(4));
  231. promisesArray.push(promiseTRSANSG(6));
  232. promisesArray.push(promiseTRSANSG(5));
  233. promisesArray.push(promiseTRJANSG(3));
  234. promisesArray.push(promiseTRSANSG(4));
  235. var promisesRace = Promise.race(promisesArray);
  236. promisesRace.then(function(values) {
  237. console.timeEnd("Promise.race");
  238. console.log("The fasted promise resolved", values);
  239. });
  240. promisesRace.catch(function(reason) {
  241. console.timeEnd("Promise.race");
  242. console.log("The fastest promise rejected with the following reason ", reason);
  243. });
Add Comment
Please, Sign In to add comment