Skip to content

How to return a custom type with methods from an async function? #454

Answered by joseluisq
joseluisq asked this question in Q&A
Discussion options

You must be logged in to vote

It worked by calling into_lua on the custom data type before returning it like

let fetch = lua.create_async_function(|_, uri: String| async move {
    let resp = http_client::get(&uri).await.into_lua_err()?;
    // ...
    let status = resp.status().as_u16();
    Ok(LuaClientResponse(status).into_lua(lua))
})

And calling the method like this

local resp = fetch("http://localhost/xyz")
print(resp:status())
-- 200

Thanks!

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by joseluisq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants