Skip to content

Commit bda0337

Browse files
committed
refactor(semaphore): change from setInterval to setTimeout avoiding multiple chaining Promises
In order to create a good usage of resources, this change use the Interval Alarm from the JavaScript implementation instead of creating multiple Promises on stale for acquiring the Semaphore.
1 parent accc448 commit bda0337

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/semaphore.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ const Semaphore = ({ resources = 1, start = resources } = {}) => {
1010
}
1111

1212
return new Promise(resolve => {
13-
setTimeout(() => {
14-
acquire().then(() => {
13+
const acquireInterval = setInterval(() => {
14+
if (counter > 0) {
15+
counter -= 1;
16+
clearInterval(acquireInterval);
17+
1518
resolve();
16-
});
19+
}
1720
}, timespan() * 1000);
1821
});
1922
};

0 commit comments

Comments
 (0)