Skip to content

Commit

Permalink
throat queue function should support a return value
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Nov 29, 2022
1 parent b24c188 commit 705b100
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/ThroatQueueFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function ThroatQueueFunction(n = 5){
while(running.length >= n){
await race(cancellationState, running)
}

return d.promise
}

ret = Q.canceller(ret)
Expand Down
30 changes: 30 additions & 0 deletions test/throat_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 705b100

Please sign in to comment.