Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 43a17cb

Browse files
author
Michelle Tilley
committed
Make queue resilient to sync errors
1 parent ff96a4d commit 43a17cb

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/async-queue.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ class Task {
88
});
99
}
1010

11-
execute() {
12-
return this.fn.call(undefined).then(this.resolve, this.reject);
11+
async execute() {
12+
try {
13+
const value = await this.fn.call(undefined);
14+
this.resolve(value);
15+
} catch (err) {
16+
this.reject(err);
17+
}
1318
}
1419

1520
runsInParallel() {
@@ -55,14 +60,9 @@ export default class AsyncQueue {
5560
this.nonParallelizableOperation = true;
5661
}
5762

58-
try {
59-
await task.execute();
60-
} catch (err) {
61-
// nothing
62-
} finally {
63-
this.threadsInUse--;
64-
this.nonParallelizableOperation = false;
65-
this.processQueue();
66-
}
63+
await task.execute();
64+
this.threadsInUse--;
65+
this.nonParallelizableOperation = false;
66+
this.processQueue();
6767
}
6868
}

test/async-queue.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,21 @@ describe('AsyncQueue', function() {
128128
await assert.async.isTrue(tasks[4].started); // both can start since they are parallelizable
129129
assert.isTrue(tasks[5].started);
130130
});
131+
132+
it('continues to work when tasks throw synchronous errors', async function() {
133+
const queue = new AsyncQueue({parallelism: 1});
134+
135+
const p1 = queue.push(() => {
136+
throw new Error('error thrown from task 1');
137+
});
138+
const p2 = queue.push(() => {
139+
return new Promise(res => res(2));
140+
});
141+
142+
try {
143+
await p1;
144+
throw new Error('expected p1 to be rejectd');
145+
} catch (err) {}
146+
assert.equal(await p2, 2);
147+
});
131148
});

0 commit comments

Comments
 (0)