I've been experimenting a bit with Typescript, but I'm now a bit stuck on how to use async/await effectively.
I'm inserting a bunch of records into a database, and I need to get the list of IDs that are returned by each insert. The following simplified example works in general, but it is not quite as elegant as I'd like and it is completely sequential.
async function generatePersons() {
const names = generateNames(firstNames, lastNames);
let ids = []
for (let name of names) {
const id = await db("persons").insert({
first_name: name.firstName,
last_name: name.lastName,
}).returning('id');
ids.push(id[0])
}
return ids
}
I tried to use map
to avoid creating the ids
list manually, but I could get this to work.
What I'd also like to have is a limited amount of parallelism. So my asynchronous calls should happen in parallel up to a certain limit, e.g. I'd only ever like to have 10 open requests, but not more.
Is there a reasonably elegant way of achieving this kind of limited parallelism with async/await in Typescript or Javascript ES7? Or am I trying to get this feature to do something it was not intended for?
PS: I know there are bulk insert methods for databases, this example is a bit artificial as I could use those to work around this specific problem. But it made me wonder about the general case where I don't have predefined bulk methods available, e.g. with network requests
Checkout the async-parallel library which offers various helper functions that make it easy to perform parallel operations. Using this library your code could look something like this...
async function generatePersons(): Promise<number[]> {
const names = generateNames(firstNames, lastNames);
return await Parallel.map(names, async (name) =>
await db("persons").insert({
first_name: name.firstName,
last_name: name.lastName,
}).returning('id'));
}
If you want to limit the number of instances to say four at-a-time you can simply do the following...
Parallel.concurrency = 4;
Psst, there's a package that does this for you on npm called p-map
Promise.all
will allow you to wait for all requests to stop finishing, without blocking their creation.
However, it does sound like you want to block sometimes. Specifically, it sounded like you wanted to throttle the number of requests in flight at any given time. Here's something I whipped up (but haven't fully tested!)
async function asyncThrottledMap<T, U>(maxCount: number, array: T[], f: (x: T) => Promise<U>) {
let inFlight = new Set<Promise<U>>();
const result: Promise<U>[] = [];
// Sequentially add a Promise for each operation.
for (let elem of array) {
// Wait for any one of the promises to complete if there are too many running.
if (inFlight.size >= maxCount) {
await Promise.race(inFlight);
}
// This is the Promise that the user originally passed us back.
const origPromise = f(elem);
// This is a Promise that adds/removes from the set of in-flight promises.
const handledPromise = wrap(origPromise);
result.push(handledPromise);
}
return Promise.all(result);
async function wrap(p: Promise<U>) {
inFlight.add(p);
const result = await p;
inFlight.delete(p);
return result;
}
}
Above, inFlight
is a set of operations that are currently taking place.
The result
is an array of wrapped Promise
s. Each of those wrapped promises basically adds or removes operations from the set of inFlight
operations. If there are too many in-flight operations, then this uses Promise.race
for any one of the in-flight operations to complete.
Hopefully that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With