Limited parallelism with async/await in Typescript/ES7
Asked Answered
D

3

10

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

Danczyk answered 28/8, 2016 at 20:20 Comment(7)
"Parallelism" is when 2 execution context run simultaneously (possibly on 2 different computation units). You cannot get that in JS.Foolery
possible duplicate of Slowdown due to non-parallel awaiting of promises?Milepost
Have a look at Using async/await with a forEach loopMilepost
Regarding limiting concurrency, that should be a separate question. Have a look at this though (hint: there's no elegant way in native promises)Milepost
Are any of the answers below acceptable solutions?Hansiain
Does this answer your question? Throttle amount of promises open at a given timeMillsap
Library iter-ops can do it nicely, with its waitRace operator.Medrek
H
10

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 Promises. 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.

Hansiain answered 29/8, 2016 at 1:6 Comment(1)
Oh my, forgot what I was saying. I had to wrap my head around this asynchronous loop that sometimes awaits and sometimes not first, and the name "wrap" led me down the wrong track. This should work out indeed.Milepost
B
3

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;
Burglarize answered 29/8, 2016 at 2:36 Comment(1)
No, concurrency can also be specified at the function level which takes precedence over the global setting. Example: await Parallel.map(..., ..., {concurrency: 4}). Default concurrency is zero which is unlimited.Burglarize
T
-1

Is there a reasonably elegant way of achieving this kind of limited parallelism with async/await in Typescript or Javascript ES7

You will have to use Promise.all. i.e. collect all the promises in an array and await Promise.all([all,the,stuff]).

More

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Thalamencephalon answered 29/8, 2016 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.