UNPKG

enqueu

Version:

Promise queue for concurrency control

47 lines (44 loc) 1.13 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../../dist/index.js"></script> </head> <body> <button>Click me up</button> <button>Click me up without enqueu</button> </body> <script> 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 () { console.log(count); r(count); }, count * 1000) ) } ) ); document.querySelectorAll("button")[1].addEventListener( "click", function(){ count = count/2; return new Promise((r) => setTimeout(function () { console.log(count); r(count); }, count * 1000) ) } ); </script> </html>