Last active
May 31, 2018 08:36
-
-
Save ypetya/59f01b978dde538efa06205ad9dd8c18 to your computer and use it in GitHub Desktop.
Simple functional chain process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cp = require('child_process'); | |
const execute = string => new Promise((resolve, reject)=>{ | |
console.log(string); | |
cp.exec(string, (err, stdout, stderr) => { | |
if (err) { | |
reject(err); | |
} | |
const out = stdout.toString().trim(); | |
console.log(out); | |
console.error(stderr.toString().trim()); | |
resolve(out); | |
}); | |
}); | |
const call = (fn, input) => new Promise((resolve,reject) => { | |
const outcome = fn(input); | |
if(typeof outcome === 'string') { | |
console.log(`Output: ${outcome}`); | |
} else if (typeof outcome === 'function') { | |
reject(outcome()); | |
return; | |
} | |
resolve(outcome); | |
}) | |
const runChild = (goal, input) => | |
typeof goal === 'string' ? | |
execute(goal) : | |
call(goal, input); | |
const chainProcess = (goal1, parent) => ({ | |
run: input => parent ? | |
parent.run(input).then(o => runChild(goal1, o)) : | |
runChild(goal1,input), | |
chain: childGoal => chainProcess(childGoal, chainProcess(goal1, parent)) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment