This is a basic implemention of Twisted's inlineCallbacks for Luvit. Instead of using Deferreds, we are forced to use functions to signal the completion of an aync event.
A callback will automagically be supplied if you yield a function which will resume the coroutine when it is fired. lic.inlined_callbacks takes a function as its only argument and will return a function which will take a callback as its first argument.
###Examples
#!/usr/bin/env luvitlocaltimer=require('timer')
localcoroutine=require('coroutine')
locallic=require('./init')
-- very thin wrapper around coroutine.yieldlocalyield=lic.yieldlocaltest=lic.inline_callbacks(function()
localres=0-- sync using wrapperres=yield(res+1)
-- sync if you like blue highlighted textres=coroutine.yield(res+1)
-- coroutines are first class citizens in luares= (function() returnyield(res+1) end)()
-- and asynclocalf=function(a, cb)
timer.setTimeout(0, cb, a+1)
endres=yield(f, res)
-- async with args is tricky cause lua is stupidlocalf2=function(a, b, cb)
returncb(a+1)
end-- you must pass in the sentinel value if the final arg can be nil--because there is no way to tell it exists otherwiseres=yield(f2, res, nil, lic.SENTINEL)
return {nil, res}
end)
localcb=function(err, res)
iferrthenreturnp('there was an err', err)
endp('the result is: ', res)
endtest(cb)
localfailure=lic.inline_callbacks(function()
-- throwing an error with return the err to our cb functionres=yield(2)
error('oh shit')
end)
failure(cb)###... and the results
$ luvit ./example.lua
"there was an err""/racker/luvit-inlineCallbacks/example.lua:50: oh shit""the result is: " 5Notice that no magic takes place (ie, the results are written out of order).