Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. // `series` Method Example
  2.  
  3. function eatFreeBread (bread, callback) {
  4. setTimeout(function () {
  5. console.log('Just ate some ' + bread + ' bread');
  6. callback(null, {breadRating: 8});
  7. }, 3000);
  8. }
  9.  
  10. function eatAppetizers (apps, callback) {
  11. setTimeout(function () {
  12. console.log('Just ate these great appetizers ' + apps);
  13. callback(null, {appRating: 9});
  14. }, 3000);
  15. }
  16.  
  17. function eatMainCourse (mainCourse, callback) {
  18. setTimeout(function () {
  19. console.log('Just ate the ' + mainCourse + ' main course');
  20. callback(null, {mainCourseRating: 9});
  21. }, 3000);
  22. }
  23.  
  24. function eatDessert (dessert, callback) {
  25. setTimeout(function () {
  26. console.log('Just ate the ' + dessert + ' dessert');
  27. callback(null, {dessertRating: 10});
  28. }, 3000);
  29. }
  30.  
  31. function handlePoorService () {
  32. console.log('WE ARE DONE WITH THIS NON SENSE AND ARE LEAVING!')
  33. }
  34.  
  35. function goOutToEatV1 (bread, apps, mainCourse, dessert, restaurantReviewCallback) {
  36.  
  37. eatFreeBread(bread, function (err, breadResult) {
  38. if (err) {
  39. handlePoorService();
  40. restaurantReviewCallback(err);
  41. } else {
  42. eatAppetizers(apps, function (err, appsResult) {
  43. if (err) {
  44. handlePoorService();
  45. restaurantReviewCallback(err);
  46. } else {
  47. eatMainCourse(mainCourse, function (err, mainCourseResult) {
  48. if (err) {
  49. handlePoorService();
  50. restaurantReviewCallback(err);
  51. } else {
  52. eatDessert(dessert, function (err, dessertResult) {
  53. if (err) {
  54. handlePoorService();
  55. restaurantReviewCallback(err);
  56. } else {
  57. restaurantReviewCallback(null, [
  58. breadResult,
  59. appsResult,
  60. mainCourseResult,
  61. dessertResult
  62. ]);
  63. }
  64. });
  65. }
  66. });
  67. }
  68. });
  69. }
  70. });
  71. }
  72.  
  73. function goOutToEatV2 (bread, apps, mainCourse, dessert, restaurantReviewCallback) {
  74. async.series([
  75. function (callback) {
  76. eatFreeBread(bread, callback);
  77. },
  78. function (callback) {
  79. eatAppetizers(apps, callback);
  80. },
  81. function (callback) {
  82. eatMainCourse(mainCourse, callback);
  83. },
  84. function (callback) {
  85. eatDessert(dessert, callback);
  86. },
  87. ], function (err, restaurantResults) {
  88. if (err) {
  89. handlePoorService();
  90. }
  91. restaurantReviewCallback(err, restaurantResults);
  92. })
  93. }
  94.  
  95. function goOutToEatV3 (bread, apps, mainCourse, dessert, restaurantReviewCallback) {
  96. async.series([
  97. _.curry(eatFreeBread)(bread),
  98. _.curry(eatAppetizers)(apps),
  99. _.curry(eatMainCourse)(mainCourse),
  100. _.curry(eatDessert)(dessert),
  101. ], function (err, restaurantResults) {
  102. if (err) {
  103. handlePoorService();
  104. }
  105. restaurantReviewCallback(err, restaurantResults);
  106. })
  107. }
  108.  
  109. const bread = 'garlic';
  110. const apps = ['rockefeller oysters', 'spinach and artichoke dip', 'bruschetta'];
  111. const mainCourse = 'filet mignon';
  112. const dessert = 'cheesecake';
  113. const restaurantReviewCallback = function (err, results) { console.log('done with our review! ' + JSON.stringify(results)); };
  114.  
  115. // Uncomment to run different versions of the solution
  116. // goOutToEatV1(bread, apps, mainCourse, dessert, restaurantReviewCallback);
  117. // goOutToEatV2(bread, apps, mainCourse, dessert, restaurantReviewCallback);
  118. // goOutToEatV3(bread, apps, mainCourse, dessert, restaurantReviewCallback);
  119.  
  120. // `each` Method Example
  121. function sendEmail (emailAddress, email, callback) {
  122. setTimeout(function () {
  123. console.log('Sent the following email ' + email + ' to the following email address ' + emailAddress);
  124. callback(null, {emailId: Math.floor(Math.random() * 10000)});
  125. }, 3000);
  126. }
  127.  
  128. function sendBatchOfEmailsV1 (emailAddresses, emailBody, callback) {
  129. var emailHasFailed = false,
  130. emailAddressIdx = 0,
  131. emailIds = [];
  132.  
  133. while (!emailHasFailed && emailAddressIdx < emailAddresses.length) {
  134.  
  135. sendEmail(emailBody, emailAddresses[emailAddressIdx], function (err, result) {
  136. if (err) {
  137. emailHasFailed = true;
  138. return callback(err);
  139. } else {
  140. emailIds.push(result.emailId);
  141. }
  142.  
  143. if (emailIds.length === emailAddresses.length) {
  144. callback(null, emailIds);
  145. }
  146. });
  147.  
  148. emailAddressIdx += 1;
  149. }
  150. }
  151.  
  152. function sendBatchOfEmailsV2 (emailAddresses, emailBody, callback) {
  153. async.map(emailAddresses, function (emailAddress, asyncCallback) {
  154. sendEmail(emailBody, emailAddress, asyncCallback);
  155. }, callback);
  156. }
  157.  
  158. function sendBatchOfEmailsV3 (emailAddresses, emailBody, batchCallback) {
  159. async.map(emailAddresses, _.curry(sendEmail)(email), batchCallback);
  160. }
  161.  
  162. const email = 'Async.js Hello World!';
  163. const emailAddresses = [
  164. 'jdoe0@gmail.com',
  165. 'jdoe1@gmail.com',
  166. 'jdoe2@gmail.com',
  167. 'jdoe3@gmail.com',
  168. 'jdoe4@gmail.com',
  169. 'jdoe5@gmail.com',
  170. 'jdoe6@gmail.com',
  171. 'jdoe7@gmail.com',
  172. 'jdoe8@gmail.com',
  173. 'jdoe9@gmail.com'
  174. ];
  175. const batchEmailCallback = function (err, emailIds) { console.log('Batch email send result = ' + JSON.stringify(emailIds)); };
  176.  
  177. // Uncomment to run different versions of the solution
  178. // sendBatchOfEmailsV1(emailAddresses, email, batchEmailCallback);
  179. // sendBatchOfEmailsV2(emailAddresses, email, batchEmailCallback);
  180. // sendBatchOfEmailsV3(emailAddresses, email, batchEmailCallback);
  181.  
  182. // `waterfall` Method Example
  183. function makeReservation (reservationDetails, callback) {
  184. setTimeout(function () {
  185. console.log('Made the following reservation ' + JSON.stringify(reservationDetails));
  186. callback(null, Math.floor(Math.random() * 10000));
  187. }, 3000);
  188. }
  189.  
  190. function processCreditCardForReservation (creditCardDetails, reservationId, callback) {
  191. setTimeout(function () {
  192. console.log('Charged the following reservation ' + reservationId + ' to the following card ' + JSON.stringify(creditCardDetails));
  193. callback(null, Math.floor(Math.random() * 10000));
  194. }, 3000);
  195. }
  196.  
  197. function emailAirlineTicketReceipt (emailAddress, receiptId, callback) {
  198. setTimeout(function () {
  199. console.log('Emailed a receipt for receipt ID ' + receiptId + ' to the following email address ' + emailAddress);
  200. callback(null, Math.floor(Math.random() * 10000));
  201. }, 3000);
  202. }
  203.  
  204. function bookAirlineTicketV1 (reservationDetails, creditCardDetails, receiptEmailAddress, bookAirlineTicketCallback) {
  205. makeReservation(reservationDetails, function (reservationErr, reservationId) {
  206. if (reservationErr) {
  207. bookAirlineTicketCallback(reservationErr);
  208. } else {
  209. processCreditCardForReservation(creditCardDetails, reservationId, function (creditCardProcessingError, receiptId) {
  210. if (creditCardProcessingError) {
  211. bookAirlineTicketCallback(creditCardProcessingError);
  212. } else {
  213. emailAirlineTicketReceipt(receiptEmailAddress, receiptId, function (emailError, emailId) {
  214. bookAirlineTicketCallback(emailError);
  215. });
  216. }
  217. });
  218. }
  219. });
  220. }
  221.  
  222. function bookAirlineTicketV2 (reservationDetails, creditCardDetails, receiptEmailAddress, bookAirlineTicketCallback) {
  223. async.waterfall([
  224. function(callback) {
  225. makeReservation(reservationDetails, callback);
  226. },
  227. function(reservationId, callback) {
  228. processCreditCardForReservation(creditCardDetails, reservationId, callback);
  229. },
  230. function(receiptId, callback) {
  231. emailAirlineTicketReceipt(receiptEmailAddress, receiptId, callback);
  232. }
  233. ], bookAirlineTicketCallback);
  234. }
  235.  
  236. function bookAirlineTicketV3 (reservationDetails, creditCardDetails, receiptEmailAddress, bookAirlineTicketCallback) {
  237. async.waterfall([
  238. _.curry(makeReservation)(reservationDetails),
  239. _.curry(processCreditCardForReservation)(creditCardDetails),
  240. _.curry(emailAirlineTicketReceipt)(receiptEmailAddress),
  241. ], bookAirlineTicketCallback);
  242. }
  243.  
  244. const reservationDetails = { destination: 'Cancun, Mexico', departureDate: '05/05/2018', airline: 'Delta' };
  245. const creditCardDetails = { number: '1111111111111111', cvv: '123', zipcode: '12345' };
  246. const receiptEmailAddress = 'jdoe@gmail.com';
  247. const bookingCallback = function (err, results) { console.log('Flight has been booked!'); };
  248.  
  249. // Uncomment to run different versions of the solution
  250. // bookAirlineTicketV1(reservationDetails, creditCardDetails, receiptEmailAddress, bookingCallback);
  251. // bookAirlineTicketV2(reservationDetails, creditCardDetails, receiptEmailAddress, bookingCallback);
  252. // bookAirlineTicketV3(reservationDetails, creditCardDetails, receiptEmailAddress, bookingCallback);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement