Comments (3)
That's sort of by design to avoid the overhead of the if
as technically you just shouldn't write such code. Remember that the original intention was to receive and return tokens or instances.
from async-sema.
The example code was a bit extreme, but it's possible for this to occur accidentally.
If the developer was careless with either of the following, then there'll be more tokens than intended.
- Pairing: Not one-to-one mapping of .acquire() to .release(), especially due to nested functions or error handling
- Asynchronity: .acquire() occuring after .release() if async operations were not handled correctly
const sema = new Sema(1);
try {
// code continues even if .acquire() is waiting or failed
sema.acquire().then(x => accessDb(sema)).catch(e => {
console.error(e);
sema.release(); // unnecessary since finally{} will release it
});
} finally {
sema.release();
}
// At this point, there are 2 tokens.
accessDb(sema);
async function accessDb(sema) {
sema.acquire().then(x => {
// do something
sema.release();
});
}
I do get what you mean though. There's a lot of ways to use the semaphores incorrectly, we can't catch all of it.
But I felt that the semaphore count should at least be consistent, or give warnings if inconsistent. It is a sanity check, if you will. Otherwise it would pretty much go undetected.
from async-sema.
Yeah, that makes sense. I'm not against adding a check if it's seen as a useful addition.
from async-sema.
Related Issues (17)
- Possible p() & v() confusion HOT 2
- broken typescript definition HOT 3
- Action required: Greenkeeper could not be activated 🚨
- Add examples to README
- Make it OSS
- Intuition surrounding `nr` values
- Rate limit and throttle: Last item throttled HOT 2
- Way to cancel pending tokens HOT 3
- Some nice way of async iterating? HOT 6
- IE 11 Support HOT 3
- Args object should be optional
- Add Sema.run utility for easy task processing
- RateLimit does not work with number less than 1. HOT 2
- Uncaught TypeError: Class extends value #<Object> is not a constructor or null HOT 1
- Is it possible to make 1 request every 2 seconds? HOT 1
- What is the `nr` variable? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-sema.