Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bun & Async functions #2003

Closed
FelixKLG opened this issue Mar 16, 2024 · 1 comment
Closed

Bun & Async functions #2003

FelixKLG opened this issue Mar 16, 2024 · 1 comment

Comments

@FelixKLG
Copy link

FelixKLG commented Mar 16, 2024

I am very aware that Bun is not Node and isn't strictly supported by napi-rs, however the Bun team have on their docs that they support a majority of the Node API.

I have a simple async function in Rust which uses Reqwest to make a HTTP request:

use napi_derive::napi;

#[napi]
pub async fn get_data(user_id: String) -> String {
    let val = match reqwest::get(format!(
        "https://example.com/api/users/{}",
        user_id
    ))
    .await
    {
        Ok(val) => val,
        Err(e) => {
            println!("{:#?}", e);
            return "broken :(".to_owned();
        }
    };

    val.text().await.unwrap()
}
const mod = require('../../napi-bun/target/release/libnapi_bun.node');


(async () => {
    const fuck = await mod.getData("270150443512889344")

    console.log(fuck)
})();

When I run this via Node it works perfectly, the program waits and makes the request and I get the response back as I'd expect. However, when I run the exact same code with Bun the program just exits before any of the async code is polled, any blocking code before the async request works fine as expected but for some reason anything which requires async polling instantly closes bun. It doesn't appear to be a crash, I think it just thinks it's done.

I am aware it's not supported but I wanted to ask why might this be the case, I'm not sure why this doesn't work on Bun so I wanted to ask the people which would know better than I.

Bun's NAPI impl tracking issue: oven-sh/bun#158

@FelixKLG
Copy link
Author

nvm was a quirk with bun, just use top level async.

the (async () => {}) syntax is a little fucked in Bun, but they have a very functional top level async.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant