Skip to content

Incorrect pMapIterable behaviour #76

Open
@dmitri-gb

Description

pMapIterable doesn't adhere to the concurrency limit when the source is an async iterable and backpressure > concurrency.

Here's an example program that reproduces the issue:

import { setTimeout } from 'timers/promises';
import { pMapIterable } from 'p-map';

async function* source() {
  yield 1;
  yield 2;
  yield 3;
}

const target = pMapIterable(source(), async n => {
  console.log(`Running with ${n}...`);
  await setTimeout(1000);
  console.log(`Finished running with ${n}`);
}, {
  concurrency: 1,
  backpressure: 2
});

for await (const _ of target) { }

Current output:

Running with 1...
Finished running with 1
Running with 2...
Running with 3...
Finished running with 2
Finished running with 3

Expected output:

Running with 1...
Finished running with 1
Running with 2...
Finished running with 2
Running with 3...
Finished running with 3

As you can see, the mapper function has two instances running in parallel even though concurrency is set to 1.

FYI @Richienb

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions