npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

raco

v3.2.7

Published

Generator based flow-control that supports both callback and promise

Downloads

92

Readme

raco

Generator based flow-control that supports both callback and promise.

Build Status

npm install raco

Many existing flow-control libraries such as co, assume promises to be the lowest denominator of async handling. Callback function requires promisify patch to be compatible, which creates unnecessary complication.

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

raco(fn*, [opts])

Resolves a generator function. This does not return a Promise; uncaught error will be thrown.

// import raco
var raco = require('raco')
...
raco(function * (next) {
  // yield promise
  console.log(yield Promise.resolve('foo')) // 'foo'
  try {
    yield Promise.reject(new Error('boom'))
  } catch (err) {
    console.log(err.message) // 'boom'
  }

  // yield callback
  yield setTimeout(next, 1000) // delay 1 second
  var data = yield fs.readFile('./data', next)
  yield mkdirp('/tmp/foo/bar', next)
  yield pump(
    fs.createReadStream('./foo'),
    fs.createWriteStream('./bar'),
    next
  )
})

Yieldable callback works by supplying an additional next argument. Yielding non-yieldable value pauses the current generator, until next(err, val) being invoked by callback. val passes back to yielded value, or throw if err exists.

raco(function * (next) {
  var res = yield setTimeout(() => {
    next(null, 'foo')
  }, 100)
  console.log(res) // 'foo'

  try {
    yield setTimeout(() => {
      next(new Error('boom'))
    }, 100)
  } catch (err) {
    console.log(err.message) // 'boom'
  }
})

fn = raco.wrap(fn*, [opts])

Wraps a generator function into regular function that optionally accepts callback or returns a promise.

var fn = raco.wrap(function * (arg1, arg2, next) {
  // pass arguments followed by `next`
  ...
  return arg1 + arg2
})

fn(167, 199, (err, val) => { ... }) // Use with callback

fn(167, 689) // use with promise
  .then((val) => { ... })
  .catch((err) => { ... })

raco.wrapAll(obj, [opts])

Wraps generator methods of class or object.

class App {
  * fn (next) { ... }
  * fn2 (next) { ... }
}

// wrap prototype object
raco.wrapAll(App.prototype)

var app = new App()

app.fn((err, val) => {...})
app.fn2().then(...).catch(...)

Options

Calling raco with options object makes a factory function with a set of available options:

var raco = require('raco')({
  Promise: null, // disable Promise
  yieldable: function (val, cb) {
    // custom yieldable
  },
  prepend: true // prepend or append `next` argument
})

opts.Promise

Raco uses native promise by default. This can be overridden by setting raco.Promise.

var raco = require('raco')({ Promise: require('bluebird') })

opts.prepend

By default, next(err, val) function appends to arguments fn* (args..., next). If opts.prepend set to true, generator function is called with fn* (next, args...). This can be useful for functions that accept varying numbers of arguments.

var raco = require('raco')

var fn = raco.wrap(function * (next, a, b) {
  return a + b
}, { prpend: true })

fn(1, 6, (err, val) => {
  console.log(val) // 7
})
fn(1, 6).then((val) => {
  console.log(val) // 7
})

opts.yieldable

By default, the following objects are considered yieldable:

  • Promise
  • Generator
  • Generator Function
  • Thunk

It is also possible to override the default yieldable mapper. Use with caution:

  • Takes the yielded value, returns true to acknowledge yieldable.
  • Callbackcb(err, val) to resolve the yieldable.
var raco = require('raco')({
  yieldable: (val, cb) => {
    // map array to Promise.all
    if (Array.isArray(val)) {
      Promise.all(val).then((res) => {
        cb(null, res)
      }, cb)
      return true // acknowledge yieldable
    }
    // Anything can be mapped!
    if (val === 689) {
      cb(new Error('DLLM'))
      return true // acknowledge yieldable
    }
    return false // acknowledge non-yieldable
  }
})

raco(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.message) // 'DLLM'
  }
})

License

MIT