Giter Site home page Giter Site logo

Comments (4)

brentropy avatar brentropy commented on June 9, 2024

Not currently, but its funny that you asked me about this, because I was working on supporting default in selects just a couple hours earlier. I am back to working on a v1 release that will have a few API changes, and selects were always a little but awkward so I wanted to revisit that as part of the change.

Chan will have an API similar to core/async's alts as well as a select DSL that is similar to go. The new select DSL will have support for default and will look something like this:

await chan.select(
  ch1, val1 => {
    console.log(val1)
  },
  ch2, val2 => {
    console.log(val2)
  },
  default => {
    console.log('ch1 and ch2 are empty')
  }
)

The select function take a variable number of arguments which alternate channels and callbacks. If there is an odd number or arguments it is assumed that the last argument is a default callback, and the behavior should match the default cause in a go select statement. The default callback does not receive any arguments, but in my example I had a param named default to add clarity. default will be undefined in the callback. Using that convention would be optional.

Thoughts? I have people waiting for chan to use promises instead of thunks (another part of the v1 refactor) so it shouldn't be too long before this is ready to be used.

from chan.

brentropy avatar brentropy commented on June 9, 2024

Well... I guess not default since it is a reserved word. That was just something I thought of as I was writing out the example. My brain isn't working yet this morning. I still like calling it out somehow. ¯_(ツ)_/¯,

from chan.

brentropy avatar brentropy commented on June 9, 2024

see 16db99b

I haven't written tests yet, so this could be broken, but it is the general idea.

from chan.

matthewmueller avatar matthewmueller commented on June 9, 2024

awesome! i'd personally go with one or the other if they're functionally equivalent. i think the expression syntax is a bit more powerful in core.async since you can dynamically set the switches at runtime, but totally your call.

It's a bit tough to derive a usage example just by looking at the code, but I'd imagine you could do something like this with new JS syntax:

await chan.select({
  ch1,
  val1() { console.log(val1) },
  ch2,
  val2() { console.log(val2) },
  default() { console.log('nothing on the channel') }
})

also, i think with core.async you can wait on both the put and the take so maybe the usage looks more like this:

await chan.select({
  ch1.take(),
  val1() { console.log(val1) },
  ch2.put('hi'),
  val2() { console.log(val2) },
  default() { console.log('nothing on the channel') }
})

also, i really liked the thunk implementation, but i guess we're all moving to promises haha.

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.