lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Oct 10, 2012 at 3:14 PM, Tim Caswell <tim@creationix.com> wrote:
> What did you think of the `data = await(fs.readFile(filename))` syntax I showed?

this is what i would call 'visible-wrapper coroutine threading'.  a
notch above this would be the 'prewrapped API' where all the
future-returning calls are replaced by wrapped versions.

something like this (untested, but have done something similar a
couple of lifetimes ago):
-----------------------------------
function wrapAPI(funcs, wrapper)
    local newapi = {}
    for k,f in pairs(funcs) do
        if type(f)=='function' then
            newapi[k] = function (...) return wrapper(f(...)) end
        end
    end
    return newapi
end

-- somewhere in your initialization...

local afs = wrapAPI(fs,await)

-- then you can just do

data = afs.readFile(filename)

---------------------------

in any case, to my eyes, both 'levels' are within my definition of
coroutine treading, and are readable enough.  The main advantage is
you avoid all the "function(err,data) ... end" boilerplate.

of course, i do recognize that sometimes you need the underlying power
of futures (or continuables, as you call them).  just don't want to
see them all the time.  I think you made the right call in making them
visible, but easily hideable.

-- 
Javier