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

caco

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

caco

Generator based control flow that supports both callbacks and promises

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

caco

Generator based control flow that supports both callbacks and promises.

Build Status

Many existing flow-control libraries such as co, assumes promises to be the lowest denominator of async handling. Callback functions require promisify to be compatible, which creates unnecessary complication.

In caco, both callbacks and promises are yieldable. Resulting function can also be used by both callbacks and promises. This enables a powerful control flow while maintaining simplicity.

npm install caco
var fn = caco(fn *)
var caco = require('caco')

var fn = caco(function * (next) {
  try {
    yield Promise.reject('boom') // yield promise reject throws error
  } catch (err) {
    console.log(err) // 'boom'
  }

  var foo = yield Promise.resolve('bar') // yield promise

  yield setTimeout(next, 1000) // yield callback using 'next' argument, delay 1 second

  // yield callback of form next(err, data): return data, throw if err exists
  var data = yield fs.readFile('./foo/bar', next) 

  return data
})

// Use with callback
fn(function (err, res) { })

// Use with promise
fn().then(...).catch(...)

To enable yieldable callbacks, yielding non-promise-nor-generator value pauses the current generator. Until next(err, val) being invoked by callback, where val passes back to yielded value, or throw if err exists.

Yieldables

By default, the following objects are supported for yield:

  • Promise
  • Observable
  • Generator

Caco also accepts a yield mapper function, so that one can basically yield anything.

var fn = caco(fn *, mapper)
function mapper (val, cb) {
  // map array to Promise.all
  if (Array.isArray(val)) {
    Promise.all(val).then(function (res) {
      cb(null, res)
    }, cb)
    return true // acknowledge yieldable
  }

  // Anything can be mapped!
  if (val === 689) {
    cb(new Error('DLLM'))
    return true
  }
}

caco(function * () {
  console.log(yield [
    Promise.resolve(1),
    Promise.resolve(2),
    3
  ]) // [1, 2, 3]

  // yield 689 throws error
  try {
    yield 689
  } catch (err) {
    console.log(err) // 'DLLM'
  }
}, mapper)(function (err) { })

License

MIT

FAQs

Package last updated on 13 Feb 2016

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