New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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 - npm Package Compare versions

Comparing version 0.0.0 to 0.1.0

examples/errors.js

114

chan.js
'use strict';
var call
, arg
, make;
var make
, Channel;
call = function(value, cb) {
if (value instanceof Error) {
cb(value);
/**
* Make a channel.
*
* @return {Function}
* @api public
*/
make = function make() {
var chan = new Channel();
return function(a, b) {
// yielded
if (typeof a === 'function') {
chan.get(a);
return;
}
// (err, res)
if (a === null && typeof b !== 'undefined') {
a = b;
}
// value
chan.add(a);
};
};
/**
* Initialize a `Channel`.
*
* @api priate
*/
Channel = function Channel() {
this.queue = [];
this.items = [];
};
/**
* Get an item with `cb`.
*
* @param {Function} cb
* @api private
*/
Channel.prototype.get = function(cb){
if (this.items.length > 0) {
this.call(cb, this.items.shift());
} else {
cb(null, value);
this.queue.push(cb);
}
};
arg = function(args) {
var i = 0;
if (args[0] === null && args.length > 1) {
i = 1;
/**
* Add `val` to the channel.
*
* @param {Mixed} val
* @api private
*/
Channel.prototype.add = function(val){
if (this.queue.length > 0) {
this.call(this.queue.shift(), val);
} else {
this.items.push(val);
}
return args[i];
};
make = function() {
var items = []
, queue = [];
return function(cb) {
if (typeof cb !== 'function') {
if (queue.length > 0) {
call(arg(arguments), queue.shift());
} else {
items.push(arg(arguments));
}
} else {
if (items.length > 0) {
call(items.shift(), cb);
} else {
queue.push(cb);
}
}
};
/**
* Invoke `cb` with `val` facilitate both
* `chan(value)` and the `chan(error, value)`
* use-cases.
*
* @param {Function} cb
* @param {Mixed} val
* @api private
*/
Channel.prototype.call = function(cb, val){
if (val instanceof Error) {
cb(val);
} else {
cb(null, val);
}
};
/**
* Expose `make()`.
*/
module.exports = make;
{
"name": "chan",
"version": "0.0.0",
"version": "0.1.0",
"description": "A go style channel implementation that works nicely with co",

@@ -21,2 +21,11 @@ "main": "chan.js",

"author": "Brent Burgoyne",
"contributors": [
{
"name": "Brent Burgoyne"
},
{
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
}
],
"license": "MIT",

@@ -29,4 +38,7 @@ "bugs": {

"mocha": "~1.14.0",
"expect.js": "~0.2.0"
"expect.js": "~0.2.0",
"co": "~2.3.0",
"superagent": "~0.15.7",
"co-wait": "0.0.0"
}
}
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