-
-
Save sumanssaurabh/cdaa0761b8fcc4ffe8464e7335fdb258 to your computer and use it in GitHub Desktop.
Revisions
-
gregkorossy revised this gist
Mar 14, 2018 . 1 changed file with 24 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,17 +6,19 @@ function Semaphore(max) { if (waiting.length > 0 && counter < max){ counter++; let promise = waiting.shift(); promise.resolve(); } } this.acquire = function() { if(counter < max) { counter++ return new Promise(resolve => { resolve(); }); } else { return new Promise((resolve, err) => { waiting.push({resolve: resolve, err: err}); }); } } @@ -28,7 +30,12 @@ function Semaphore(max) { this.purge = function() { let unresolved = waiting.length; for (let i = 0; i < unresolved; i++) { waiting[i].err('Task has been purged.'); } counter = 0; waiting = []; return unresolved; @@ -41,11 +48,15 @@ let sema = new Semaphore(2); async function test(id) { console.log('queueing task', id); try { await sema.acquire(); console.log('running task', id); setTimeout(() => { sema.release(); }, 2000); } catch (e) { console.error(id, e); } } test(1); @@ -64,4 +75,7 @@ setTimeout(() => { test(20); test(21); test(22); }, 2700); // PURGE TEST // setTimeout(() => {sema.purge();}, 2200); -
gregkorossy revised this gist
Mar 14, 2018 . 1 changed file with 19 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,17 +3,17 @@ function Semaphore(max) { var waiting = []; var take = function() { if (waiting.length > 0 && counter < max){ counter++; let promise = waiting.shift(); promise(); } } this.acquire = function() { if(counter < max) { counter++ return; } else { return new Promise(resolve => { waiting.push(resolve); @@ -27,8 +27,8 @@ function Semaphore(max) { } this.purge = function() { let unresolved = waiting.length; counter = 0; waiting = []; return unresolved; @@ -40,12 +40,12 @@ function Semaphore(max) { let sema = new Semaphore(2); async function test(id) { console.log('queueing task', id); await sema.acquire(); console.log('running task', id); setTimeout(() => { sema.release(); }, 2000); } test(1); @@ -55,13 +55,13 @@ test(4); test(5); setTimeout(() => { test(10); test(11); test(12); }, 1500); setTimeout(() => { test(20); test(21); test(22); }, 2700); -
gregkorossy created this gist
Mar 14, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ function Semaphore(max) { var counter = 0; var waiting = []; var take = function() { if (waiting.length > 0 && counter < max){ counter++; let promise = waiting.shift(); promise(); } } this.acquire = function() { if(counter < max) { counter++ return; } else { return new Promise(resolve => { waiting.push(resolve); }); } } this.release = function() { counter--; take(); } this.purge = function() { let unresolved = waiting.length; counter = 0; waiting = []; return unresolved; } } // testing the semaphore let sema = new Semaphore(2); async function test(id) { console.log('queueing task', id); await sema.acquire(); console.log('running task', id); setTimeout(() => { sema.release(); }, 2000); } test(1); test(2); test(3); test(4); test(5); setTimeout(() => { test(10); test(11); test(12); }, 1500); setTimeout(() => { test(20); test(21); test(22); }, 2700);