Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chan

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chan

A go style channel implementation that works nicely with co

  • 0.6.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
23K
decreased by-6.38%
Maintainers
1
Weekly downloads
 
Created
Source

Chan

A golang like channel implementation for JavaScript that works well with co.

Build Status Code Climate Dependency Status

Features

  • CSP Style channels in JavaScript
  • Buffered or Unbuffered channels
  • Channels can be closed
  • API designed to work well with generators and co
  • Can be used without generators
  • Channels can be selected similar to Go's select statement

Installation

$ npm install chan --save

The Basics

Chan is inspired by golang's channels. It is implemented as a function that represents an asynchronous first in first out queue.

var makeChan = require('chan')
// make a new unbuffered channel
var ch = makeChan()
typeof ch // -> 'function'

Sending values to the channel

Values are added to the channel by calling the function with either (value) or (error, value). The return value is a thunk (a function that take a node-style callback as its only argument). The callback given to the thunk is called once the value is added.

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
  }
})

Receiving values from the channel

Values are removed from the channel by calling it with a node-style callback as this first argument. When a value is available on the channel the callback is called with the value or error. In this case the channel itself can also be a thunk.

ch(function (err, val) {
  // called when there is a value or error on the channel
})

Generators

Because thunks are yield-able in a co generator, chan works very well when combined with co. Using them together makes chan feel very similar to go channels.

var co = require('co')

co(function *() {
  var val = yield ch
})

co(function *() {
  yield ch('foo')
})

Buffer

Docs coming soon...

Close

Docs coming soon...

Select

Docs coming soon...

Keywords

FAQs

Package last updated on 05 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc