Skip to content

Commit dc73c23

Browse files
authored
Update block_on.md
1 parent 3630d2f commit dc73c23

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/block_on.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The block_on function is an synchronous function that produces a final value of an asynchronous function, you can think of it as an adapter from the asynchronous world to the synchronous world. The block_on function is part of the tokio or async-std crates, not part of the standard library.
44

55
## Why block_on
6-
In a sense, asynchronous function just pass the buck, this buck is simply due to the fact that, when executing a synchronous function: the caller only resumes when the operation is completed, what if we want our thread to do something else while the operating system does it work, we will need to use new I/O library that provides an asynchronous version of this function, Rust approach of supporting asynchronous operation is by introducing a trait, std::future::Future. A Future represent an operation you can test for completion. So with Future, you can use the current thread and do some other job, but using futures seems challenging to use because, you keep on polling other jobs when a future is still pending, keeping track of previous futures who's pending and what should be done once its finish, somehow ruin the simplicity of the asynchronous function. Well this buck is solve using .await expression. it's true that it easy to get the value of an async function: just await it. But async function itself return a future, so it's now the caller's job to do the polling somehow, thus someone has to wait for value.
6+
In a sense, asynchronous function just pass the buck, this buck is simply the fact that, when executing a synchronous function: the caller only resumes when the operation is completed, what if we want our thread to do something else while the operating system is doing his work, we will need to use new I/O library that provides an asynchronous version of this function, Rust approach of supporting asynchronous operation is by introducing a trait, std::future::Future. A Future represent an operation you can test for completion. So with Future, you can always know the state of the current thread inother to do other jobs, but using futures seems challenging because, you keep on polling other jobs while a future is still pending, keeping track of previous futures who are pending and what should be done once there are finished and poll it again, and this somehow ruin the simplicity of the function. Good news, asynchronous function is there, this buck is solved using .await expression which pause the execution of this async function until the awaited value is ready before resuming back it execution. it's true that it easy to get the value of an async function: just await it. But async function itself return a future, so it's now the caller's job to do the polling somehow, thus someone has to wait for value and in this case block_on is our waiter.
77
Consider the example below:
88
```sh
99
use async_std::io::prelude::*;

0 commit comments

Comments
 (0)