From 705b1005bc98070e7fa3685eca309cd9c62a93d3 Mon Sep 17 00:00:00 2001 From: Mathew Heard Date: Tue, 29 Nov 2022 20:30:47 +1100 Subject: [PATCH] throat queue function should support a return value --- lib/ThroatQueueFunction.js | 2 ++ test/throat_test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/ThroatQueueFunction.js b/lib/ThroatQueueFunction.js index 1641dcd..7d6d637 100644 --- a/lib/ThroatQueueFunction.js +++ b/lib/ThroatQueueFunction.js @@ -60,6 +60,8 @@ function ThroatQueueFunction(n = 5){ while(running.length >= n){ await race(cancellationState, running) } + + return d.promise } ret = Q.canceller(ret) diff --git a/test/throat_test.js b/test/throat_test.js index c0166d1..00b9bb2 100644 --- a/test/throat_test.js +++ b/test/throat_test.js @@ -78,6 +78,36 @@ describe('ThroatQueueFunction', function(){ await tf(null) expect(count).to.be.eql(10) + }) + it('return value should work', async() => { + const tf = ThroatQueueFunction(5) + + const ps = [] + let count = 0 + const deferred = Q.defer() + for(let i = 0; i < 10; i++){ + ps.push(tf(async ()=>{ + count++ + await deferred.promise + return i + })) + } + + await Q.delay(10) + + + expect(count).to.be.eql(5) + deferred.resolve() + + + await Q.delay(10) + await tf(null) + expect(count).to.be.eql(10) + + for(let i = 0; i < 10; i++){ + expect(await ps[i]).to.be.eql(i) + } + }) it('should run 5 times only if cancelled from fn', async() => { const tf = ThroatQueueFunction(5)