ReactiveX / ReactiveX/rxjs

Allow concurrency in `concatMap`

Open
#5,519 13 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
31.7k
Forks
3k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the concatMap and mergeMap operator entry points, focusing on how mergeMap accepts its concurrent parameter. Define the concurrency behavior while preserving the original emission order, then verify that the proposed concatMap(projection, concurrent) usage works as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.