Socket
Socket
Sign inDemoInstall

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


Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

caco

Generator based control flow that supports both callbacks and promises.

Build Status

Many of the existing async libraries require wrapping callback functions into promises to be usuable, which creates unnecessary complication.

In caco, both callbacks and promises are 'yieldable'; resulting function can be used by both callbacks and promises. This enables a powerful control flow while maintaining compatibility.

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

var fn = caco(function * (next) {
  var foo = yield Promise.resolve('bar') // yield promise
  yield setTimeout(next, 100) // yield callback using 'next' argument

  // try/catch errors
  try {
    yield Promise.reject('boom')
  } catch (err) {
    console.log(err) // 'boom'
  }

  var data = yield fs.readFile('./foo/bar', next)
  return data
})

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

fn().then(...).catch(...)

API

var fn = caco(fn *)

Wraps a generator into a regular function that acceots callback or promise.

var getN = caco(function * (n, next) {
  if (n === 689) yield Promise.reject('boom') // yield reject throws error
  return yield Promise.resolve(n)
})

getN(123, function (err, val) {
  console.log(val) // 123
})
getN(123).then(...)

getN(689).catch(function (err) {
  console.log(err) // boom
})

Generator accepts optional next argument for yieldable callback

License

MIT

FAQs

Package last updated on 11 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