Promise queue for concurrency control
$ npm i enqueu
or
<script src="https://unpkg.com/enqueu@1.0.0/dist/index.js"></script>
In this case you will get "hey, my name is yoann, how are you ?, bye" and without Enqueu "my name is yoann, bye, how are you ?, hey".
import Enqueu from "enqueu";
const queu = new Enqueu();
queu
.add(() => new Promise((r) => setTimeout(r, 3000)))
.then((_) => console.log("hey"));
queu
.add(() => new Promise((r) => setTimeout(r, 2000)))
.then((_) => console.log("how are you ?"));
queu
.add(() => new Promise((r) => setTimeout(r, 1000)))
.then((_) => console.log("bye"));
queu
.add(() => new Promise((r) => setTimeout(r, 500)), { priority: 1 }) //from 1 to Infinity
.then((_) => console.log("my name is Yoann"));
For example in this case if you click 4 times at once on the button you will get "10, 5, 2.5". But without Enqueu you will get "1.25, 1.25, 1.25, 1.25".
const queu = new Enqueu({ maxSize: 3 }); //the maximum size of the queu is 3 (other will be throwed off)
let count = 20;
document.querySelector("button").addEventListener(
"click",
queu.createFn(function () {
count = count / 2;
return new Promise((r) =>
setTimeout(function () {
r(count);
}, count * 1000)
);
})
);
maxSize
Max size of the queumaxConcurrency
Max promise as the same time
isPaused
Return a boolean to see if the queu is paused or notpending
Return the number of pending functionsqueu
Return the queu
add(fn: Function, options: Options = {}): Function
Add the function to the queucreateFn(fn: Function, options: Options = {}): Function
Create a function that will add "fn" to the queu on call (useful for addEventListener for example)pause()
Pause the queustart()
Start the queu after a pauseonEmpty(fn: Function)
Call the function passed as argument when the queu is emptyonQueuElementExecuted(fn: Function)
Call the function passed as argument when the a new queu function is startedonQueuElementFinishExecution(fn: Function)
Call the function passed as argument when the a new queu function which started is finishedclear()
Clear the queuremove(fn: Function)
Remove a function from the queuremoveFromIndex(index: number)
Remove a specified indexed element in the queu