Skip to content

Instantly share code, notes, and snippets.

@catharinejm
Last active August 29, 2015 14:17
Show Gist options
  • Save catharinejm/667c5fb5f8daa4fa5efc to your computer and use it in GitHub Desktop.
Save catharinejm/667c5fb5f8daa4fa5efc to your computer and use it in GitHub Desktop.
fs = require 'fs'
#request = require 'request'
#
request = ({uri}, done) ->
done null, uri
reqreq = (uris) ->
for uri in uris
yield ( (done) -> done null, uri )
_ = (f, args...) ->
(args2...) -> f args..., args2...
filestuff = (fnames) ->
for name in fnames
(yield _ fs.readFile, name, encoding: 'utf8').toUpperCase()
doreq = ->
yield _ request, uri: 'http://google.com'
GeneratorFunctionPrototype = (-> yield null)().constructor
doAsync = (f, done) ->
it = f()
try cur = it.next() catch err then return done err
iter = (cur, it) ->
if cur.done then return done null, cur.value
task =
if cur.value instanceof GeneratorFunctionPrototype
console.log 'yield*'
_ doAsync, (-> cur.value)
else
cur.value
task (err, result) ->
if err then return done err
try nc = it.next result catch err then return done err
iter nc, it
iter cur, it
doAsync (->
bigone = [0..100000]
yield reqreq(bigone)
# [
# yield doreq()
# yield filestuff(bigone)
# ]
), _(console.log, "RESULT")
# doAsync _(filestuff, process.argv[2..]...), _(console.log, "RESULT")
# doAsync _(doreq), _(console.log, "RESULT")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment