Skip to content

Commit 410192a

Browse files
committed
slightly better way to handle makeExclusiveReturning
1 parent fc3d722 commit 410192a

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/shared/semaphore.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,19 @@ class Semaphore {
7272

7373
/**
7474
* Take an async function and turn it into an exclusively executing async function. Calls during async execution will
75-
* be returned.
75+
* get a promise for the result of the exclusive caller.
7676
*/
7777
static makeExclusiveReturning(cb) {
7878
let isRunning;
79+
let runningPromise;
7980
return async (...args) => {
8081
if (isRunning) {
81-
return;
82+
return runningPromise;
8283
}
8384
isRunning = true;
8485
try {
85-
return await cb(...args);
86+
runningPromise = cb(...args);
87+
return await runningPromise;
8688
} finally {
8789
isRunning = false;
8890
}

test/shared/semaphore.test.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ describe("semaphore", () => {
99
let executionLog;
1010
const n = 3;
1111
const m = 2;
12-
const result = "finished";
1312
const runner = async (index) => {
1413
executionLog.push(format("started %d", index));
1514
await sleep(10);
1615
executionLog.push(format("finished %d", index));
17-
return result;
16+
return format("result %d", index);
1817
};
1918

2019
beforeEach(() => {
@@ -27,15 +26,15 @@ describe("semaphore", () => {
2726
const resultsSecondary = await Promise.all(Array.from({ length: m }, (_, i) => runner(n + i + 1)));
2827
expect(resultsPrimary).toMatchInlineSnapshot(`
2928
[
30-
"finished",
31-
"finished",
32-
"finished",
29+
"result 1",
30+
"result 2",
31+
"result 3",
3332
]
3433
`);
3534
expect(resultsSecondary).toMatchInlineSnapshot(`
3635
[
37-
"finished",
38-
"finished",
36+
"result 4",
37+
"result 5",
3938
]
4039
`);
4140
expect(executionLog).toMatchInlineSnapshot(`
@@ -61,15 +60,15 @@ describe("semaphore", () => {
6160
const resultsSecondary = await Promise.all(Array.from({ length: m }, (_, i) => exclusiveRunner(n + i + 1)));
6261
expect(resultsPrimary).toMatchInlineSnapshot(`
6362
[
64-
"finished",
65-
"finished",
66-
"finished",
63+
"result 1",
64+
"result 2",
65+
"result 3",
6766
]
6867
`);
6968
expect(resultsSecondary).toMatchInlineSnapshot(`
7069
[
71-
"finished",
72-
"finished",
70+
"result 4",
71+
"result 5",
7372
]
7473
`);
7574
expect(executionLog).toMatchInlineSnapshot(`
@@ -95,15 +94,15 @@ describe("semaphore", () => {
9594
const resultsSecondary = await Promise.all(Array.from({ length: m }, (_, i) => exclusiveRunner(n + i + 1)));
9695
expect(resultsPrimary).toMatchInlineSnapshot(`
9796
[
98-
"finished",
99-
undefined,
100-
undefined,
97+
"result 1",
98+
"result 1",
99+
"result 1",
101100
]
102101
`);
103102
expect(resultsSecondary).toMatchInlineSnapshot(`
104103
[
105-
"finished",
106-
undefined,
104+
"result 4",
105+
"result 4",
107106
]
108107
`);
109108
expect(executionLog).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)