Skip to content

Commit eb82a56

Browse files
committed
Update tokio fetch
1 parent cfa5f4d commit eb82a56

File tree

1 file changed

+17
-8
lines changed
  • examples/tokio-fetch/src

1 file changed

+17
-8
lines changed

examples/tokio-fetch/src/lib.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,26 @@ fn node_version(cx: &mut Cx) -> NeonResult<String> {
3131
cx.global::<JsObject>("process")?.prop(cx, "version").get()
3232
}
3333

34+
// Export an async JavaScript function where the body is executed on the tokio thread pool
35+
#[neon::export]
36+
async fn node_release_date(version: String) -> Result<String, Error> {
37+
let release = fetch_node_release(&version)
38+
.await?
39+
.ok_or_else(|| format!("Could not find version: {version}"))?;
40+
41+
Ok(release.date)
42+
}
43+
44+
// Similar to `node_release_date`, but includes some setup code synchronously executed
45+
// on the JavaScript main thread before return a task for tokio. Since this is not
46+
// an `async fn`, we need to explicitly tell the export macro that it returns a future.
3447
#[neon::export(async)]
35-
fn node_release_date(
48+
fn current_node_release_date(
3649
cx: &mut Cx,
3750
) -> NeonResult<impl Future<Output = Result<String, Error>> + use<>> {
51+
// Executes synchronously on the JavaScript main thread
3852
let version = node_version(cx)?;
3953

40-
Ok(async move {
41-
let release = fetch_node_release(&version)
42-
.await?
43-
.ok_or_else(|| format!("Could not find version: {version}"))?;
44-
45-
Ok(release.date)
46-
})
54+
// This task is executed asynchronously on the tokio thread pool
55+
Ok(node_release_date(version))
4756
}

0 commit comments

Comments
 (0)