Skip to main content

$pLimit

$pLimit(array, limit, mapFunction, keyFunction?) -> array
Compact type signature<anff?>
Execution
Works with async supplied functionsCan run in parallel

This helper runs many items with a supplied function while limiting how many items are in flight at once. The supplied function can do async work. See Parallelism And Async Evaluation and Async And Concurrency.

Source: FUME

Documentation

$pLimit() runs a supplied function across the input with a concurrency limit.

The supplied function may perform async work, but only up to limit items are in flight at once. See Parallelism And Async Evaluation for the execution model and Async And Concurrency for related helpers.

If the first argument is not already an array, FUME treats it as a one-item sequence. The limit is truncated to an integer and clamped to at least 1. Results are returned in the original input order, even though work is performed in parallel, and mapped values that are undefined are omitted from the returned sequence.

Without a key function, items are assigned to lanes round-robin. When a key function is provided, its result controls lane assignment: numbers use modulo arithmetic, strings are hashed deterministically, booleans map to lane 0 or 1, other values are stable-stringified and hashed, and null or undefined fall back to round-robin. Items within the same lane run sequentially, while different lanes can run concurrently.

Use $pLimit() when the item list is dynamic but you need to protect a remote system or avoid starting too much work at once. Use $pMap() when everything can start together, or an array constructor when the branch list is already known.

Examples

Input

This example uses the claims-lines example input. The key function groups items by claimId, so lines from the same claim stay on the same processing lane while the overall result still preserves input order.

Example input

JSON
[
{
"claimId": "clm-2001",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 110,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2001",
"lineNumber": 2,
"serviceCode": "PROC-020",
"serviceDisplay": "imaging study",
"units": "1",
"unitPrice": 350,
"allowedAmount": 300,
"modifiers": [
"MOD-B",
"MOD-C"
]
},
{
"claimId": "clm-2001",
"lineNumber": 3,
"serviceCode": "PROC-030",
"serviceDisplay": "lab panel",
"units": 2,
"unitPrice": 40,
"allowedAmount": 70,
"modifiers": []
},
{
"claimId": "clm-2002",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 115,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2002",
"lineNumber": 2,
"serviceCode": "PROC-040",
"serviceDisplay": "counseling",
"units": 3,
"unitPrice": 60,
"allowedAmount": 150,
"modifiers": [
"MOD-D"
]
}
]

Expression

$pLimit($, 2, function($l) { $l.claimId & ":" & $string($l.lineNumber) }, function($l) { $l.claimId })

Result

[
"clm-2001:1",
"clm-2001:2",
"clm-2001:3",
"clm-2002:1",
"clm-2002:2"
]