
Company News
Meet the Socket Team at RSAC and BSidesSF 2026
Join Socket for live demos, rooftop happy hours, and one-on-one meetings during BSidesSF and RSA 2026 in San Francisco.
Run a promise graph with concurrency control.
$ npm install p-graph
p-graph does not have a strict Node version requirement, but the syntax used is currently intended to remain compatible with Node 12+.
The p-graph library takes in a map of of nodes and a list of dependencies. The keys in the map are unique string identifiers for each node in the graph. The value of the map is the definition of the task, including the function that should be executed by that task in it's run argument. The dependencies list is an array of tuples, each tuple contains the two values that must correspond to ids in the node map. The run function corresponding to the first item in the tuple must complete before the second item in the tuple can begin.
The return value of pGraph is a class with a run() function. Calling the run() function will return a promise that resolves after all the tasks in the graph have finished completed. Tasks are run in dependency order.
import { pGraph, type DependencyList, type PGraphNodeRecord } from "p-graph";
// This can be either an object or map (PGraphNodeMap).
// Functions can be sync or async.
const nodeMap: PGraphNodeRecord = {
putOnShirt: { run: () => console.log("put on your shirt") },
putOnShorts: { run: () => console.log("put on your shorts") },
putOnJacket: { run: () => console.log("put on your jacket") },
putOnShoes: { run: () => console.log("put on your shoes") },
tieShoes: { run: () => console.log("tie your shoes") },
};
const dependencies: DependencyList = [
// You need to put your shoes on before you tie them!
["putOnShoes", "tieShoes"],
["putOnShirt", "putOnJacket"],
["putOnShorts", "putOnJacket"],
["putOnShorts", "putOnShoes"],
];
await pGraph(nodeMap, dependencies).run();
There are some contexts where you may want to limit the number of functions running concurrently. One example would be to prevent overloading the CPU with too many parallel tasks. The concurrency argument to run will limit the number of functions that start running at a given time. If no concurrency option is set, the concurrency is not limited and tasks are run as soon as they are unblocked.
await pGraph(graph).run({ concurrency: 3 });
By default, tasks are considered to all be equally important, so they're equally likely to be picked to run once all the tasks they depend on are complete. To control the ordering of tasks, use the priority option when defining a task node. Tasks will always execute in dependency order, but unblocked tasks with a higher priority will be favored over those with a lower priority.
const nodeMap: PGraphNodeRecord = {
highPri: { run: () => Promise.resolve(), priority: 10 },
lowPri: { run: () => Promise.resolve(), priority: 1 },
unspecified: { run: () => Promise.resolve() } // treated as 0
}
FAQs
Run a promise graph with concurrency control
We found that p-graph demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Join Socket for live demos, rooftop happy hours, and one-on-one meetings during BSidesSF and RSA 2026 in San Francisco.

Research
/Security News
Malicious Packagist packages disguised as Laravel utilities install an encrypted PHP RAT via Composer dependencies, enabling remote access and C2 callbacks.

Research
/Security News
OpenVSX releases of Aqua Trivy 1.8.12 and 1.8.13 contained injected natural-language prompts that abuse local AI coding agents for system inspection and potential data exfiltration.