Skip to content

Instantly share code, notes, and snippets.

@reklis
Created March 24, 2017 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reklis/7852370590378881c54e298e54faa188 to your computer and use it in GitHub Desktop.
Save reklis/7852370590378881c54e298e54faa188 to your computer and use it in GitHub Desktop.
example of tj style task library
const fs = require('fs')
function task(fn) {
const gen = fn()
function step(err, res) {
const ret = gen.next(res)
if (ret.done) return
ret.value(step)
}
step()
}
function read(path) {
return (cb) => {
fs.readFile(path, 'utf8', cb)
}
}
task(function *() {
const a = yield read('/root/.ssh/config')
const b = yield read('bar.txt')
})
//https://medium.com/@tjholowaychuk/callbacks-vs-coroutines-174f1fe66127
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment