zserge / lua-promises

A+ promises in Lua
https://zserge.com/lua-promises/
MIT License
222 stars 35 forks source link

Example is wrong in documentation #2

Closed Quenty closed 9 years ago

Quenty commented 9 years ago

In the documentation, we have this code:

deferred.first(
    read(somefile), -- resolves promise with contents, or rejects with error
    timeout(5),
)

Where it's expected that deferred.first(args) will take a single table. This bug can be fixed by modifying code like this:

function M.first(...)
    local args = {...}

    local d = M.new()
    for _, v in ipairs(args) do
        v:next(function(res)
            d:resolve(res)
        end, function(err)
            d:reject(err)
        end)
    end
    return d
end

return M

Or by making the readme.md use a table wrapper first.

zserge commented 9 years ago

Thanks! Initially I was using varargs. However in practice it's more convenient to keep lists of promises as tables, so varargs syntax was not so useful. That's why I replaced varargs with explicit tables in the API.

Fixed now by editing the README.md.only