Created
March 24, 2017 22:01
-
-
Save reklis/7852370590378881c54e298e54faa188 to your computer and use it in GitHub Desktop.
example of tj style task library
This file contains hidden or 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 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
https://github.com/yortus/asyncawait
https://github.com/laverdet/node-fibers