Walling-off async/await

I want to use async Rust (Tokio) in a segregated part of an app.

The fn signatures of this module will not be async. Internally, it will make async calls (reqwest). It does not need to do anything with responses other than log them, but I don't want to block the calling thread so I want to return right away.

So, I need to kick-off async functions, operate on the response, but make this transparent to the caller.

(This would, of course, be trivially simple if I could fire & forget a tokio::task with spawn, but something has to process the response.)

Is there a good pattern for such a use-case? I'm thinking there is not. Either fn async has to be viral or else I need some machinery of some sort to handle async responses.

This is not clear, does it do something or not with the response?

1 Like

Logging them is the "something." I meant that the caller doesn't care about the response; the fn that makes the request doesn't need to return it. Sorry, was unclear.

Can't that be part of the task you spawn? Something like:

tokio::spawn(async {
    let response = async_call().await;
    log(response);
});
2 Likes

Can't believe that didn't occur to me. Forgot you can await in the task itself.

If all else fails, you can always block_on the Future representing your async task/block/fn.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.