Async completion of a future via promise


#1

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
}


#2

You need oneshot: https://docs.rs/futures/0.2.1/futures/channel/oneshot/fn.channel.html

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

Thanks. That will work for me.


#4

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