Async completion of a future via promise

I am trying to understand how to construct and return an instance of Box<Future<String>> from a function, where the future is completed explicitly later on triggered by an external event. Essentially, I am looking at the rust analogy for Promise in Scala. Is the such a thing? How should I construct it in rust?

Pseudo code:

let saved_global_promise = Promise<String>::new(); // this what I would do in scala, but it does not exist in Rust

fn promise_me_now() -> Box<Future<String>> {
    return saved_global_promise.future;
}

fn complete_promise_later_explicitly() {
    promise.complete("some result text"); // activates poll for the previously returned future to complete
}

1 Like

You need oneshot: futures::channel::oneshot::channel - Rust

FWIW, this is very different from how most other languages do this, and it is indeed non-obvious how to find it. Perhaps docs could be improved to explicitly call-out analogy with promises?

3 Likes

Thanks. That will work for me.

Filed https://github.com/rust-lang-nursery/futures-rs/pull/1040

1 Like