Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Generator based flow-control that supports both callback and promise.
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(...)
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:
It is also possible to override the default yieldable mapper. Use with caution:
true
to acknowledge yieldable.cb(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'
}
})
MIT
FAQs
Generator based flow-control that supports both callback and promise
The npm package raco receives a total of 22 weekly downloads. As such, raco popularity was classified as not popular.
We found that raco demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.