MCPcopy
hub / github.com/cujojs/when / n

Function n

guard.js:48–69  ·  view source on GitHub ↗

* Creates a condition that allows only n simultaneous executions * of a guarded function * @param {number} allowed number of allowed simultaneous executions * @returns {function} condition function which returns a promise that * fulfills when the critical section may be entered. The fulfil

(allowed)

Source from the content-addressed store, hash-verified

46 * section has been exited.
47 */
48 function n(allowed) {
49 var count = 0;
50 var waiting = [];
51
52 return function enter() {
53 return when.promise(function(resolve) {
54 if(count < allowed) {
55 resolve(exit);
56 } else {
57 waiting.push(resolve);
58 }
59 count += 1;
60 });
61 };
62
63 function exit() {
64 count = Math.max(count - 1, 0);
65 if(waiting.length > 0) {
66 waiting.shift()(exit);
67 }
68 }
69 }
70
71});
72}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));

Callers

nothing calls this directly

Calls 1

resolveFunction · 0.70

Tested by

no test coverage detected