Comments (5)
I am glad you like chan
. I can see how the select functionality could be useful. I will look in to adding something like this. Thanks for the suggestion Eugene.
from chan.
Hi Brent, I actually had another idea about how to achieve the same result, as it's more a synchronization issue of multiple yieldables in co, so I wrote a simple module called co-select
that can do the job:
var co = require('co'),
select = require('co-select'),
chan = require('chan');
co(function *(){
var ch1 = chan(),
ch2 = chan();
var first = yield select([ch1, ch2]);
switch (first.caller) {
case ch1:
var val1 = first.value;
break;
case ch2:
var val2 = first.value;
break;
}
})();
If you just want the first value from a channel, you can pass true
through as the last argument:
var co = require('co'),
select = require('co-select'),
chan = require('chan');
co(function *(){
var ch1 = chan(),
ch2 = chan();
var firstValue = yield select([ch1, ch2], true);
})();
from chan.
Oops. Spoke too soon. co-select
won't actually solve the issue, because if a value from ch1
is read, then we lose the future value from ch2
.
So, probably needs to be implemented in the chan
library where you have knowledge of the channel state and don't have to actually read from all the channels in advance to determine which one is ready to be read.
from chan.
I had a crack at adding select()
to chan
in #8
from chan.
Resolved by pull request #9. Merged.
from chan.
Related Issues (8)
- example with emitters HOT 2
- push a generator/generator function to a channel? HOT 1
- error handling HOT 2
- Buffered channel HOT 2
- doc: an example doesn't work
- do chan.select defaults work? HOT 4
- Why do I run a while loop when I use Chan and jump out of the function after the loop without executing the code after the loop?
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 chan.