Skip to content

Commit

Permalink
ignore error during throat queue function
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Nov 29, 2022
1 parent 8a4bce1 commit 8e6ea59
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/ThroatQueueFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const NextTickPromise = Q()

function ThroatQueueFunction(n = 5){
const running = []
async function race(cancellationState, running){
const deferred = Q.defer()
cancellationState.deferredWrap(deferred)
running = [...running, deferred.promise]
await Q.safeRace(running)
}
let ret = async function(cancellationState, what){
if(what === null){
if(running.length === 0) return running
Expand All @@ -13,8 +19,10 @@ function ThroatQueueFunction(n = 5){

// This shouldn't happen if we correctly await on the throat
while(running.length >= n){
await cancellationState.promiseWrap(Q.cancelledRace(running))
await race(cancellationState, running)
}

const idObj = {}

// call fn
const rFn = async ()=>{
Expand All @@ -34,18 +42,23 @@ function ThroatQueueFunction(n = 5){
}
}

const idObj = {}
const r = rFn()
r.id = idObj
r.fn = what
r.cancel = ()=>{
const d = Q.defer()
const rr = d.promise
rr.id = idObj
rr.fn = what
rr.cancel = ()=>{
cancellationState.cancel()
}
running.push(r)
await r

running.push(rr)
try {
await r
} finally {
r.then(d.resolve, d.reject)
}

while(running.length >= n){
await cancellationState.promiseWrap(Q.cancelledRace(running))
await race(cancellationState, running)
}
}

Expand Down

0 comments on commit 8e6ea59

Please sign in to comment.