Async Queue (Throttler). For rate limiting. For example, you use an external API and the number of requests per second is limited there.
- Fast: Uses very fast algorithms for storage (BinHeap). The complexity of the any operation is no more log(n);
- Simple to use
- Can calculate remains time and limits usage
- Can use priority
- Can abort items in Queue
const Queue = require('hkey-async-queue');
const queue = new Queue({delay: 30}); // OR new Queue(30);
//There will be a 30ms delay between element calls.
await queue.push();
const promise = queue.push();
await promise2;
console.log();
const queue = new Queue({delay: 30}); // OR new Queue(30);
console.log(queue.length);
console.log(queue.usage() * 100 + '%');
console.log(queue.remains()); //remains waiiting time in queue;
await queue.tillEnd();
const promise = queue.push();
console.log(promise.remains()); //remains waiting time in queue for this promise
console.log(promise.indexOf()); //count items before promise
promise.resolve('hello');
promise.reject(new Error('reject'));
Promise throws AbortError
promise.abort();
const queue = new Queue({
delay : 30,
defaultPriority : 50,
}); //OR new Queue(30, 50)
queue.push(101).then(()=>console.log(101));
queue.push(10).then(()=>console.log(10));
queue.push(100).then(()=>console.log(100));
//101, 100, 10