Giter Site home page Giter Site logo

Comments (5)

brentropy avatar brentropy commented on June 2, 2024

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.

eugeneware avatar eugeneware commented on June 2, 2024

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.

eugeneware avatar eugeneware commented on June 2, 2024

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.

eugeneware avatar eugeneware commented on June 2, 2024

I had a crack at adding select() to chan in #8

from chan.

brentropy avatar brentropy commented on June 2, 2024

Resolved by pull request #9. Merged.

from chan.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.