Promise.reject equivalent in rust tokio async?

I need Promise.resolve and Promise.reject javascript equivalent in async rust. I'm using Tokio btw.
Thanks

Promise.reject looks like it's just futures::future::ready(Err(x)). Promise.resolve would be futures::future::ready(Ok(x)).

ready function: ready in futures::future - Rust

1 Like

Rust's equivalent is quite different, because Rust doesn't have built-in success or failure in its Futures, and generally doesn't use callbacks for them.

The closest equivalent is to return a type like Result<TypeResolved, TypeRejected>, and if you need to do that via callback, then use a channel:

and it will be channel.send(Ok(resolved)) or channel.send(Err(rejected)).

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.