Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Question:
- // If getFoo and getBar were web calls, would more than one web call ever be in flight at the same time?
- // If so, will there be multiple foo's going on, multiple bars going on, or a mix of the two?
- async function getFoo(value) {
- console.log('getFoo called');
- return new Promise((resolve, reject) => {
- setTimeout(() => {
- console.log('getFoo resolving');
- resolve(value + ': 29');
- }, 2500);
- });
- }
- async function getBar(value) {
- console.log('getBar called');
- return new Promise((resolve, reject) => {
- setTimeout(() => {
- console.log('getBar resolving');
- resolve(value + ': 29');
- }, 5000);
- });
- }
- function test() {
- const values = [
- 'alpha',
- 'bravo',
- 'charlie'
- ];
- console.log('test method called. About to invoke reduce');
- return values.reduce(async (promise, value, index) => {
- console.log(`reduce method invoked for index=${index} : value=${value}`);
- const oldResult = await promise;
- console.log(`finished await on promise accum : index=${index} : value=${value}`);
- const fooResult = await getFoo(value);
- console.log(`finished await on getFoo accum : index=${index} : value=${value}`);
- const barResult = await getBar(value);
- console.log(`finished await on getBar accum : index=${index} : value=${value}`);
- const newResult = Promise.resolve([fooResult, barResult, ...oldResult]);
- console.log(`created result. Returning from reduce func : index=${index} : value=${value}`);
- return newResult;
- }, Promise.resolve([]));
- }
- console.log('Booting');
- const result = test();
- console.log('Returned from test function. Exiting test. Stuff is going to keep running');
Add Comment
Please, Sign In to add comment