MCPcopy Create free account
hub / github.com/firebase/firebase-tools / pLimit

Function pLimit

src/utils.ts:1150–1184  ·  view source on GitHub ↗
(concurrency: number)

Source from the content-addressed store, hash-verified

1148 * A lightweight Promise concurrency limiter.
1149 */
1150export function pLimit(concurrency: number): Limit {
1151 if (!Number.isInteger(concurrency) || concurrency <= 0) {
1152 throw new FirebaseError(`pLimit concurrency must be a positive integer, got ${concurrency}`);
1153 }
1154
1155 const queue: Array<() => void> = [];
1156 let activeCount = 0;
1157
1158 const next = () => {
1159 activeCount--;
1160 if (queue.length > 0) {
1161 queue.shift()?.();
1162 }
1163 };
1164
1165 return <T>(fn: () => Promise<T>): Promise<T> => {
1166 return new Promise<T>((resolve, reject) => {
1167 const run = () => {
1168 activeCount++;
1169 try {
1170 Promise.resolve(fn()).then(resolve, reject).finally(next);
1171 } catch (err) {
1172 reject(err);
1173 next();
1174 }
1175 };
1176
1177 if (activeCount < concurrency) {
1178 run();
1179 } else {
1180 queue.push(run);
1181 }
1182 });
1183 };
1184}
1185
1186/**
1187 * Calculates the Levenshtein distance between two strings.

Callers 3

constructorMethod · 0.90
findSourceMapMappingsFunction · 0.90
uploadSourceMapsFunction · 0.90

Calls 1

runFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…