Limited parallelism with async/await in Typescript/ES7

Solution 1:

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.

Solution 2:

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;