caolan / async

Async utilities for node and the browser
http://caolan.github.io/async/
MIT License
28.18k stars 2.41k forks source link

Passing promise as collection #1750

Closed gamevnlc closed 3 years ago

gamevnlc commented 3 years ago

Hi everyone I want to pass a list of promises to parallel, but it shows the error Error: expected a function. Is there any way to do it. Thanks

aearly commented 3 years ago

You don't need Async for this.

await Promise.all(listOfPromises)

or

Promise.all(listOfPromises).then((results) => {})
gamevnlc commented 3 years ago

@aearly yes I knew it, just wondering is it possible to use promise with Async.Parallel

aearly commented 3 years ago

Passing it a list of async functions will work, but you're still better off using Promise.all.

fierydrake commented 3 years ago

Darn, I was hoping to do this with parallel() since it is a bit nicer than Promise.all() because it accepts an object as well as an array.

For example, I have something like this (with many more functions each with more args in reality):

async function getNames(a, b, c) { ... }
async function getPages(a, b, c) { ... }

async function buildCombined(with, some, args) {
  const combined = await async.parallel({
    names: async () => getNames(with, some, args),
    pages: async () => getPages(with, some, args),
  });
  return combined;
}

It would be nice to remove the async () => and have parallel handle Promise values directly.

aearly commented 3 years ago

You should already be able to do that: https://github.com/caolan/async/blob/master/test/es2017/asyncFunctions.js#L473-L482

fierydrake commented 3 years ago

Either I did not explain clearly, or I do not understand the code you referenced in your answer. If I remove the async wrapper function like below, I don't think it works?

async function getNames(a, b, c) { ... }
async function getPages(a, b, c) { ... }

async function buildCombined(with, some, args) {
  const combined = await async.parallel({
    names: getNames(with, some, args),
    pages: getPages(with, some, args),
  });
  return combined;
}