Skip to content

Allow concurrency in concatMap #5519

@JosephSilber

Description

@JosephSilber

Feature Request

Is your feature request related to a problem? Please describe.

We have mergeMap which can map values concurrently, but the new values it produces are not in the original order. If you want to maintain order, you must use concatMap, but concatMap doesn't run concurrently (it's equivalent to calling mergeMap with concurrent set to 1).

Describe the solution you'd like

It would be great if concatMap could take a concurrent parameter, just like mergeMap already does:

concatMap(projection, concurrent)

Describe alternatives you've considered

I don't really have a full-on alternative. In my particular use-case the inner observables only produced a single value (they're Promises), so I was able to build a convoluted workaround. See the next section.

Additional context

Here's my use-case:

I'm shipping a bunch of orders, and then printing their shipping label.

Here are those functions:

function shipOrder(orderId: number) : Promise<ShippingLabel> {
    return api.orders.ship(orderId);
}

function printLabel(label: ShippingLabel) : Promise<void> {
    return printer.print(label);
}

Now, I want to ship multiple orders at once:

from(orderNumbers)
    .pipe(mergeMap(shipOrder, 5))
    .pipe(printLabel);

...but I want the shipping labels to be printed in their original sort order.

This is where concatMap with concurrency would help:

from(orderNumbers)
    .pipe(concatMap(shipOrder, 5))
    .pipe(printLabel);

The orders would still be shipped in parallel, but the labels will all print in the correct order.


Here's a StackOverflow question and answer with way more details:

https://stackoverflow.com/questions/57045892/rxjs-mergemap-with-original-order

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions