How to resolve a future from within another function?

Sometimes I want to release the result from an async function from another place in the code.
Usually in JS we have Promise.resolve and Promise.reject.
The Resolve and Reject can be stored anywhere and called on later by some other function.
This can be useful if I want some async message API between threads and listen to incoming messages. The incoming thread response has a specific ID, I can then run the resolve at that ID.


let resolve_storage = HashMap

async fn send_message(){
 let  id = create_message_id();
 resolve_storage.insert(message_id, SOME_RESULT_CALLER );
}
fn incoming_message(message_id:string){
   
   resolve_storage.get(message_id).resolve(result);

   if error resolve_storage.get(message_id).reject(error);
}

 send_message().await?
 send_message().await?
 send_message().await?

How is this pattern approached in Rust? What are the diffculties? Is there something better?
Especially using async_std

It sounds like you're looking for an oneshot channel?

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.