Translate EventEmitter events into an error-first callback result.
Funnel EventEmitter events into an error-first callback result.
Delta is part of the Cadence universe. The examples below use Cadence, but this would also be useful for programming in Streamline.js or other error-first callback domininate flow-control libraries.
I use Delta to gather the results of
EventEmitter objects and funnel them into
an error-first callback.
var delta = require'delta' children = require'child_process'var ps = childrenspawn'ps' 'ax'var delta =if error throw errorif code != 0throw 'ps failed'return stdoutjoin''split/\n/length - 2deltaeepsstdouton'data'eepsstderron'data'console.errorchunktoStringeepson'close'
var children = require'child_process' cadence = require'cadence'var processCount = cadencevar ps = childrenspawn'ps' 'ax'asyncasynceepsstdouton'data'eepsstderron'data'console.errorchunktoStringeepson'close'if code != 0throw 'ps failed'return stdoutjoin''split/\n/length - 2processCountif error throw errorconsole.log'processes: ' + count
It is complicated, but not really. Let's look closer at the
function to see all the ways in which
Delta gathers an
var processCount = cadence// run `ps`var ps = childrenspawn'ps' 'ax'// gather the output from `ps` and wait for it to finish, count the lines to// count the number of processes running on this machine.async// wrap a callback in a `Delta` object, the results from the// `EventEmitter` are fed to the callback.var delta = async// gather stdout into an array, the first argument to the callback.deltaeepsstdouton'data'// set a handler for stderr, no argument is passed to the callback.deltaeepsstderron'data'console.errorchunktoString// wait for the process to close, the code and signal are passed to the// callback.deltaeepson'close'if code != 0throw 'ps failed'return stdoutjoin''split/\n/length - 2