diff --git a/lib/ThroatQueueFunction.js b/lib/ThroatQueueFunction.js index 7d6d637..17f6b0d 100644 --- a/lib/ThroatQueueFunction.js +++ b/lib/ThroatQueueFunction.js @@ -52,16 +52,16 @@ function ThroatQueueFunction(n = 5){ } running.push(rr) try { - await r - } finally { - r.then(d.resolve, d.reject) + d.resolve(await r) + } catch(ex) { + d.reject(ex) } while(running.length >= n){ await race(cancellationState, running) } - return d.promise + return await d.promise } ret = Q.canceller(ret) diff --git a/test/throat_test.js b/test/throat_test.js index 00b9bb2..414bd69 100644 --- a/test/throat_test.js +++ b/test/throat_test.js @@ -134,6 +134,9 @@ describe('ThroatQueueFunction', function(){ deferred.resolve(true) await tf(null) + + await Q.delay(10) + expect(count).to.be.eql(5) }) @@ -193,4 +196,61 @@ describe('ThroatQueueFunction', function(){ expect(e.stack).to.contain('testFn') } }) + + + it('should capture stack trace (2)', async() => { + async function testFn(){ + const errors = [] + const tf = ThroatQueueFunction(5) + + for(let i = 0; i < 10; i++){ + try { + await tf(async ()=>{ + await Q.delay(1) + throw new Error('test') + }) + } catch(ex) { + errors.push(ex) + } + } + + return errors + } + + const errors = await testFn() + expect(errors.length).to.be.eql(10) + for(const e of errors){ + expect(e.stack).to.be.a('string') + expect(e.stack).to.contain('testFn') + } + }) + it('should capture stack trace (3)', async() => { + async function testFn(){ + const errors = [] + const tf = ThroatQueueFunction(5) + + for(let i = 0; i < 10; i++){ + const p = tf(async ()=>{ + await Q.delay(1) + throw new Error('test') + }) + errors.push(p.catch(ex=>ex)) + } + + const ret = [] + for(let e of errors){ + e = await e + ret.push(e) + } + + return ret + } + + const errors = await testFn() + expect(errors.length).to.be.eql(10) + for(let e of errors){ + expect(e.stack).to.be.a('string') + expect(e.stack).to.contain('testFn') + } + }) }) \ No newline at end of file