brentropy / chan Goto Github PK
View Code? Open in Web Editor NEWA go style channel implementation for JavaScript that works well with co
A go style channel implementation for JavaScript that works well with co
something like this?
var ch = chan();
co(function*() {
fs.readFile('file', ch);
try {
var contents = yield ch;
} catch(err) {
// handle the fs error
}
});
basically the default
cases here: https://gobyexample.com/non-blocking-channel-operations
couldn't really see anything from the code, but in the original PR with the switch statement makes me think that it's possible
co(gen)(ch)
could be simplified by a method like:
ch.push(gen)
or something. only real benefit is that you won't have to require('co')
all the time in addition to require('chan')
. not really a big deal - you can close if you don't want it :P
i see problems with this
, so meh
i.e. if i want to do fs.createWriteStream().on('finish', ch).on('error', ch)
note: i'm opening up an issue instead of a PR because i expect there to be tests :P
const chan = require('chan');
const ch = chan();
async function parts(fn) {
return new Promise(function (resolve, reject) {
ch(function (err, res) {
if (err) return reject(err)
resolve(res);
})
})
}
let count = 0;
const tick=setInterval(async () => {
if(count==3){
clearInterval(tick);
}else{
count++;
await ch(count);
}
}, 4000);
async function fun() {
let part;
const a = await 4;
while (
(part =await parts()
) != null
) {
console.log(part);
}
console.log(a);
}
fun()
//console
//1
//2
//3
console.log(a); doesn't work
While reading the doc at https://github.com/brentburg/chan#sending-values-to-the-channel, it appears that this code should execute the callback:
ch('foo')(function (err) {
if (err) {
// There was an error putting the value on the channel
} else {
// The value was successfully put on the channel
}
})
In fact, it doesn't execute the callback (when ch = makeChan()
).
Did you mean a buffered channel here?
P.S. Or maybe add a "yield ch" block that would make it work?
Your channels are co-compatible unlike js-csp, that's cool.
Is it possible to create buffered channels that behave similarly to their go counter parts?
Hi Brent,
Really loving this module. It's great to be able to use CSP-style concurrency in node.js!
The only roadblock I've found when using chan
is that there's no easy way to be able to do the equivalent of a select.
Ideally I'd love to be able to write the following co code:
var co = require('co'),
chan = require('chan');
co(function *(){
var ch1 = chan(),
ch2 = chan();
var ch = yield chan.select([ch1, ch2]);
switch (ch) {
case ch1:
var val1 = yield ch1;
break;
case ch2:
var val2 = yield ch2;
break;
}
})();
There are lots of times where being able to use multiple channels (such as one for a timeout for example) is really useful, and currently there is no easy way to atomically wait for activity on multiple channels.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.