Skip to content

Commit 0b67a18

Browse files
committed
async in JavaScript
1 parent c52fadc commit 0b67a18

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

part2/async/intro.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Blocking vs. Non-blocking
2+
-------------------------
3+
4+
In synchronous programming, the program will wait for a response from the
5+
operating system before continuing. This is known as blocking. In contrast,
6+
asynchronous programming allows the program to continue executing other
7+
operations while waiting for a response from the operating system. This is
8+
known as non-blocking.
9+
10+
11+
What is async?
12+
---------------
13+
14+
Asynchronous programming is a style of programming in which operations are
15+
performed independently, in parallel, and asynchronously. This is in contrast
16+
to synchronous programming, where operations are performed one after the other,
17+
in sequence.
18+
19+
In this section, we will look at how to write asynchronous code in JavaScript.
20+
21+
22+
What is an async function?
23+
--------------------------
24+
25+
An async function is a function that returns a coroutine. A coroutine is a
26+
special type of object that can be used to suspend a function's execution and
27+
resume it later. When a coroutine is suspended, it can be resumed later, and
28+
when it is resumed, it picks up where it left off.
29+
30+
The `async` keyword is used to declare a function as an async function. An
31+
async function can be called as a regular function, but it can also be called
32+
as a coroutine. When called as a coroutine, the function will return a
33+
coroutine object. The coroutine object can be used to suspend the function's
34+
execution and resume it later.
35+
36+
Here is an example of an async function:
37+
38+
```js
39+
async function foo() {
40+
console.log('foo');
41+
}
42+
```
43+
44+
When this function is called, it will return a coroutine object. The coroutine
45+
object can be used to suspend the function's execution and resume it later.
46+
47+
```js
48+
const coro = foo();
49+
coro.next();
50+
```
51+
52+
This will print `foo` to the console, and then suspend the function's
53+
execution.

0 commit comments

Comments
 (0)