Constructor
new Semaphore(maxopt)
Example
// Some async function that takes time to execute
function fn(x) {
return new Promise(resolve => {
setTimeout(() => {
console.log(x);
resolve(x);
}, 1000));
}
};
// Create a Semaphore and add many concurrent tasks
const semaphore = new Semaphore(1);
for ( let i of Array.fromRange(100) ) {
semaphore.add(fn, i);
}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
max |
number |
<optional> |
1
|
The maximum number of tasks which are allowed concurrently. |
Members
active :number
The number of actively executing tasks
Type:
- number
max :number
The maximum number of tasks which can be simultaneously attempted.
Type:
- number
remaining :number
The number of pending tasks remaining in the queue
Type:
- number
Methods
add(fn, …argsopt) → {Promise}
Add a new tasks to the managed queue
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn |
function | A callable function |
|
args |
* |
<optional> <repeatable> |
Function arguments |
Returns:
A promise that resolves once the added function is executed
- Type
- Promise
clear()
Abandon any tasks which have not yet concluded