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 - npm Package Compare versions

Comparing version 2.0.3 to 2.0.5

15

index.js
var isGeneratorFunction = require('is-generator-function')
var cball = require('callback-all')

@@ -26,3 +27,4 @@ function isGenerator (val) {

var ticking = false
var callback
var parallel = cball()
var callback = null

@@ -71,3 +73,3 @@ // pass caco next to generator function

} else {
// if another next() called during pausing,
// if another next() called during ticking,
// break iter and callback

@@ -78,2 +80,11 @@ iter = null

next.push = function () {
return parallel()
}
next.all = function () {
parallel(next)
parallel = cball() // reset parallel
}
if (callback) {

@@ -80,0 +91,0 @@ step()

3

package.json
{
"name": "caco",
"version": "2.0.3",
"version": "2.0.5",
"description": "Generator based control flow that supports both callbacks and promises",

@@ -20,4 +20,5 @@ "scripts": {

"dependencies": {
"callback-all": "^1.0.0",
"is-generator-function": "^1.0.3"
}
}

@@ -89,2 +89,46 @@ # caco

#### `next.push()`, `next.all()`
caco also provides a simple mechanism to maintain parallel callbacks:
* `next.push()` a callback into a parallel queue.
* `yield next.all()` aggregates callback result into an Array,
following the sequence of `next.push()` being called.
This also resets the parallel queue.
```js
var caco = require('caco')
function asyncFn = function (val, cb) {
setTimeout(cb, Math.random() * 100, null, val)
}
function asyncFnErr = function (err, cb) {
setTimeout(cb, Math.random() * 100, err)
}
caco(function * (next) {
asyncFn(1, next.push())
asyncFn(2, next.push())
asyncFn(3, next.push())
console.log(yield next.all()) // [1, 2, 3]
asyncFn(4, next.push())
asyncFn(5, next.push())
console.log(yield next.all()) // [4, 5]
asyncFn(6, next.push())
asyncFnErr(new Error('boom'), next.push())
asyncFn(8, next.push())
asyncFn(9, next.push())
try {
yield next.all()
} catch (err) {
console.log(err.message) // 'boom'
}
}).catch(function (err) {
// handle uncaught error
})
```
## Yieldable

@@ -140,31 +184,4 @@

## Aggregated Yield
Multiple results can be aggregated in one `yield` by using `Promise.all` or [callback-all](https://github.com/cshum/callback-all).
```js
var caco = require('caco')
var cball = require('callback-all')
caco(function * (next) {
// Promise.all
var promises = [
asyncFn1(), // foo
asyncFn2() // bar
]
console.log(yield Promise.all(promises)) // ['foo', 'bar']
// callback-all
var all = cball()
asyncFn1(all()) // foo
asyncFn2(all()) // bar
console.log(yield all(next)) // ['foo', 'bar']
}).catch(function (err) {
// handle uncaught error
})
```
## License
MIT

@@ -185,1 +185,43 @@ var test = require('tape')

function cbRes (timeout, res, cb) {
setTimeout(cb, timeout, null, res)
}
function cbErr (timeout, err, cb) {
setTimeout(cb, timeout, err)
}
test('next.push() and next.all()', function (t) {
t.plan(5)
caco(function * (next) {
t.deepEqual(yield next.all(), [], 'next.all() empty')
cbRes(20, 1, next.push())
cbRes(10, 2, next.push())
cbRes(30, 3, next.push())
cbRes(0, 4, next.push())
t.deepEqual(
yield next.all(),
[1, 2, 3, 4],
'push and all'
)
cbRes(20, 6, next.push())
cbRes(10, 7, next.push())
cbRes(30, 8, next.push())
t.deepEqual(
yield next.all(),
[6, 7, 8],
'push and all multiple'
)
t.deepEqual(yield next.all(), [], 'next.all() empty')
}).catch(t.error)
caco(function * (next) {
cbRes(20, 1, next.push())
cbErr(10, 2, next.push())
cbRes(30, 3, next.push())
cbErr(0, 4, next.push())
yield next.all()
}).then(t.error).catch(function (err) {
t.equal(4, err, 'push and all error')
})
})
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