Guest User

Untitled

a guest
Oct 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. // Question:
  2. // If getFoo and getBar were web calls, would more than one web call ever be in flight at the same time?
  3. // If so, will there be multiple foo's going on, multiple bars going on, or a mix of the two?
  4. async function getFoo(value) {
  5. console.log('getFoo called');
  6. return new Promise((resolve, reject) => {
  7. setTimeout(() => {
  8. console.log('getFoo resolving');
  9. resolve(value + ': 29');
  10. }, 2500);
  11. });
  12. }
  13.  
  14. async function getBar(value) {
  15. console.log('getBar called');
  16. return new Promise((resolve, reject) => {
  17. setTimeout(() => {
  18. console.log('getBar resolving');
  19. resolve(value + ': 29');
  20. }, 5000);
  21. });
  22. }
  23.  
  24.  
  25. function test() {
  26. const values = [
  27. 'alpha',
  28. 'bravo',
  29. 'charlie'
  30. ];
  31. console.log('test method called. About to invoke reduce');
  32. return values.reduce(async (promise, value, index) => {
  33. console.log(`reduce method invoked for index=${index} : value=${value}`);
  34. const oldResult = await promise;
  35. console.log(`finished await on promise accum : index=${index} : value=${value}`);
  36.  
  37. const fooResult = await getFoo(value);
  38. console.log(`finished await on getFoo accum : index=${index} : value=${value}`);
  39. const barResult = await getBar(value);
  40. console.log(`finished await on getBar accum : index=${index} : value=${value}`);
  41.  
  42. const newResult = Promise.resolve([fooResult, barResult, ...oldResult]);
  43. console.log(`created result. Returning from reduce func : index=${index} : value=${value}`);
  44. return newResult;
  45. }, Promise.resolve([]));
  46. }
  47.  
  48. console.log('Booting');
  49. const result = test();
  50. console.log('Returned from test function. Exiting test. Stuff is going to keep running');
Add Comment
Please, Sign In to add comment