Skip to content

Commit 84898c1

Browse files
authored
Touch up documentation about futures (#1753)
1 parent 3c887c4 commit 84898c1

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

guide/src/reference/js-promises-and-rust-futures.md

+9
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ Learn more:
1313

1414
[crate]: https://crates.io/crates/wasm-bindgen-futures
1515
[docs]: https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/
16+
17+
## Compatibility with versions of `Future`
18+
19+
The current crate on crates.io, `wasm-bindgen-futures 0.4.*`, supports
20+
`std::future::Future` and `async`/`await` in Rust. This typically requires Rust
21+
1.39.0+ (as of this writing on 2019-09-05 it's the nightly channel of Rust).
22+
23+
If you're using the `Future` trait from the `futures` `0.1.*` crate then you'll
24+
want to use the `0.3.*` track of `wasm-bindgen-futures` on crates.io.

guide/src/wasm-bindgen-test/asynchronous-tests.md

+20-23
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,34 @@ like fetching resources and/or other bits and pieces. To accommodate this
55
asynchronous tests are also supported through the `futures` and
66
`wasm-bindgen-futures` crates.
77

8-
To write an asynchronous test:
9-
10-
1. Change `#[wasm_bindgen_test]` into `#[wasm_bindgen_test(async)]`
11-
12-
2. Change the return type of the test function to `impl Future<Item = (), Error
13-
= JsValue>`
14-
15-
The test will pass if the future resolves without panicking or returning an
16-
error, and otherwise the test will fail.
17-
18-
## Example
8+
Writing an asynchronous test is pretty simple, just use an `async` function!
9+
You'll also likely want to use the `wasm-bindgen-futures` crate to convert JS
10+
promises to Rust futures.
1911

2012
```rust
21-
extern crate futures;
22-
extern crate js_sys;
23-
extern crate wasm_bindgen_futures;
24-
25-
use futures::Future;
2613
use wasm_bindgen::prelude::*;
2714
use wasm_bindgen_futures::JsFuture;
2815

29-
#[wasm_bindgen_test(async)]
30-
fn my_async_test() -> impl Future<Item = (), Error = JsValue> {
16+
#[wasm_bindgen_test]
17+
async fn my_async_test() {
3118
// Create a promise that is ready on the next tick of the micro task queue.
3219
let promise = js_sys::Promise::resolve(&JsValue::from(42));
3320

3421
// Convert that promise into a future and make the test wait on it.
35-
JsFuture::from(promise)
36-
.map(|x| {
37-
assert_eq!(x, 42);
38-
})
39-
.map_err(|_| unreachable!())
22+
let x = JsFuture::from(promise).await.unwrap();:
23+
assert_eq!(x, 42);
4024
}
4125
```
26+
27+
## Rust compiler compatibility
28+
29+
Note that `async` functions are only supported in stable from Rust 1.39.0 and
30+
beyond. As of the time of this writing (2019-09-05) this is the Nightly channel
31+
of Rust.
32+
33+
If you're using the `futures` crate from crates.io in its 0.1 version then
34+
you'll want to use the `0.3.*` version of `wasm-bindgen-futures` and the `0.2.8`
35+
version of `wasm-bindgen-test`. In those modes you'll also need to use
36+
`#[wasm_bindgen_test(async)]` instead of using an `async` function. In general
37+
we'd recommend using the nightly version with `async` since the user experience
38+
is much improved!

guide/src/wasm-bindgen-test/usage.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
### Add `wasm-bindgen-test` to Your `Cargo.toml`'s `[dev-dependencies]`
44

5-
Make sure to replace "X.Y.Z" with the same version of `wasm-bindgen` that you
6-
have in the `[dependencies]` section!
7-
85
```toml
96
[dev-dependencies]
10-
wasm-bindgen-test = "X.Y.Z"
7+
wasm-bindgen-test = "0.3.0"
118
```
129

10+
Note that the `0.3.0` track of `wasm-bindgen-test` supports Rust 1.39.0+, which
11+
is currently the nightly channel (as of 2019-09-05). If you want support for
12+
older compilers use the `0.2.*` track of `wasm-bindgen-test`.
13+
1314
## Write Some Tests
1415

1516
Create a `$MY_CRATE/tests/wasm.rs` file:

0 commit comments

Comments
 (0)