pub fn wrap_async<'a>(
    lua: &'a Lua,
    async_fn: LuaFunction<'a>,
    schedule_fn: LuaFunction<'a>,
) -> LuaResult<LuaFunction<'a>> {
    let pending = pending(lua)?;
    lua.load(chunk! {
        return function(...)
            local args = {...}
            local cb = table.remove(args)

            assert(type(cb) == "function", "Invalid type for cb")
            local schedule = function(...) return $schedule_fn(...) end

            // Wrap the async function in a coroutine so we can poll it
            local thread = coroutine.create(function(...) return $async_fn(...) end)

            // Start the future by peforming the first poll
            local status, res = coroutine.resume(thread, unpack(args))

            local inner_fn
            inner_fn = function()
                // Thread has exited already, so res is an error
                if not status then
                    cb(false, res)
                // Got pending status on success, so we are still waiting
                elseif res == $pending then
                    // Resume the coroutine and then schedule a followup
                    // once it has completed another round
                    status, res = coroutine.resume(thread)
                    schedule(inner_fn)
                // Got success with non-pending status, so this should be the result
                else
                    cb(true, res)
                end
            end
            schedule(inner_fn)
        end
    })
    .eval()
}
/// Wraps an async function and a scheduler function such that
/// a new function is returned that takes a callback when the async
/// function completes as well as zero or more arguments to provide
/// to the async function when first executing it
///
/// ```lua
/// local f = wrap_async(some_async_fn, schedule_fn)
/// f(arg1, arg2, ..., function(success, res) end)
/// ```
Wraps an async function and a scheduler function such that
a new function is returned that takes a callback when the async
function completes as well as zero or more arguments to provide
to the async function when first executing it


Wraps an async function and a scheduler function such that a new function is returned that takes a callback when the async function completes as well as zero or more arguments to provide to the async function when first executing it
pub fn wrap_async<'a>(
    lua: &'a Lua,
    async_fn: LuaFunction<'a>,
    schedule_fn: LuaFunction<'a>,
) -> LuaResult<LuaFunction<'a>> {
    let pending = pending(lua)?;
    lua.load(chunk! {
        return function(...)
            local args = {...}
            local cb = table.remove(args)
            assert(type(cb) == "function", "Invalid type for cb")
            local schedule = function(...) return $schedule_fn(...) end
            // Wrap the async function in a coroutine so we can poll it
            local thread = coroutine.create(function(...) return $async_fn(...) end)
            // Start the future by peforming the first poll
            local status, res = coroutine.resume(thread, unpack(args))
            local inner_fn
            inner_fn = function()
                // Thread has exited already, so res is an error
                if not status then
                    cb(false, res)
                // Got pending status on success, so we are still waiting
                elseif res == $pending then
                    // Resume the coroutine and then schedule a followup
                    // once it has completed another round
                    status, res = coroutine.resume(thread)
                    schedule(inner_fn)
                // Got success with non-pending status, so this should be the result
                else
                    cb(true, res)
                end
            end
            schedule(inner_fn)
        end
    })
    .eval()
}
pub fn wrap_async < ' a > ( lua : & ' a Lua , async_fn : LuaFunction < ' a > , schedule_fn : LuaFunction < ' a > , ) -> LuaResult < LuaFunction < ' a > > { let pending = pending ( lua ) ? ; lua . load ( chunk ! { return function ( ) local args { } local cb table remove ( args ) assert ( type ( cb ) " " " " ) local schedule function ( ) return $ schedule_fn ( ) end // Wrap the async function in a coroutine so we can poll it local thread coroutine create ( function ( ) return $ async_fn ( ) end ) // Start the future by peforming the first poll local status res coroutine resume ( thread unpack ( args ) ) local inner_fn inner_fn function ( ) // Thread has exited already, so res is an error if not status then cb ( false res ) // Got pending status on success, so we are still waiting elseif res $ pending then // Resume the coroutine and then schedule a followup // once it has completed another round status res coroutine resume ( thread ) schedule ( inner_fn ) // Got success with non-pending status, so this should be the result else cb ( true res ) end end schedule ( inner_fn ) end } ) . eval ( ) }
Wraps an async function and a scheduler function such that
a new function is returned that takes a callback when the async
function completes as well as zero or more arguments to provide
to the async function when first executing it
Wraps an async function and a scheduler function such that a new function is returned that takes a callback when the async function completes as well as zero or more arguments to provide to the async function when first executing it
lua
&'a Lua
async_fn
LuaFunction<'a>
schedule_fn
LuaFunction<'a>