Skip to content

Commit d4a3a12

Browse files
committed
async and await
1 parent e84fa03 commit d4a3a12

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Tutorial
2+
--------
3+
4+
The `async` and `await` keywords in JavaScript are used to make asynchronous programming easy,
5+
by introducing something called **coroutines**. A coroutine is a function which can pause
6+
its execution and return control to the main loop until some event occurs. It is an alternative
7+
approach for using callback functions, which makes it easier to write, understand and maintain.
8+
9+
### The `await` keyword
10+
11+
The `await` keyword is a special command which tells JavaScript to stop the execution of the
12+
current function until a Promise resolves, and then return the promise's value. It can be
13+
seen as an endless loop which checks if the promise has been resolved, and returns the value
14+
of the resolved promise when it does.
15+
16+
The `await` keyword only works inside `async` functions (which are coroutines, as explained before).
17+
The tricky part about `async` functions is that they return a Promise, instead of a value. This
18+
means that every time we need to run an `async` function, we need to `await` on it if we want
19+
to get the return value.
20+
21+
Let's revisit the example of `sumAsync` from the Promises tutorial, but with an addition:
22+
The sleep function will return a `Promise` which resolves after `ms` milliseconds.
23+
24+
function sleep(ms) {
25+
return new Promise((resolve) => setTimeout(resolve, ms));
26+
}
27+
28+
function sumAsync(x, y) {
29+
return new Promise(function(resolve, reject) {
30+
sleep(500).then(() => {
31+
resolve(x + y);
32+
});
33+
});
34+
}
35+
36+
// let's use the function now
37+
sumAsync(5, 7).then(function(result) {
38+
console.log("The result of the addition is:", result);
39+
});
40+
41+
Now, let's rewrite sumAsync to wait 500 milliseconds using the sleep function. We can make our
42+
code `sumAsync` much nicer by simply using `await` on the `sleep` function and then
43+
returning the result.
44+
45+
function sleep(ms) {
46+
return new Promise((resolve) => setTimeout(resolve, ms));
47+
}
48+
49+
async function sumAsync(x, y) {
50+
// this code waits here for 500 milliseconds
51+
await sleep(500);
52+
// done waiting. let's calculate and return the value
53+
return x+y;
54+
}
55+
56+
// sumAsync is an async function, which means it returns a Promise.
57+
sumAsync(5, 7).then(function(result) {
58+
console.log("The result of the addition is:", result);
59+
});
60+
61+
Since `sumAsync` is a an `async` function, it **implicitly** returns a `Promise`, just like
62+
the previous example which **explicitly** returns a `Promise`. The two `sumAsync` functions
63+
are completely identical in their functionality, but the one which is defined using `async`
64+
is much easier to understand!
65+
66+
Exercise
67+
--------
68+
69+
Write an async function which waits 500 milliseconds and then returns the uppercase
70+
of a given string. Use the `sleep` function provided.
71+
72+
Tutorial Code
73+
-------------
74+
function sleep(ms) {
75+
return new Promise((resolve) => setTimeout(resolve, ms));
76+
}
77+
78+
async function uppercaseString(s) {
79+
// your code goes here
80+
}
81+
82+
uppercaseString("edward").then(console.log);
83+
84+
Expected Output
85+
---------------
86+
EDWARD
87+
88+
Solution
89+
--------
90+
function sleep(ms) {
91+
return new Promise((resolve) => setTimeout(resolve, ms));
92+
}
93+
94+
async function uppercaseString(s) {
95+
await sleep(500);
96+
return s.toUpperCase();
97+
}
98+
99+
uppercaseString("edward").then(console.log);

tutorials/learn-js.org/en/Welcome.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ learn-js.org is still under construction - If you wish to contribute tutorials,
2626
### Advanced tutorials
2727

2828
- [[Promises]]
29+
- [[Async and Await]]
2930
- [[Object Oriented JavaScript]]
3031
- [[Function Context]]
3132
- [[Inheritance]]

0 commit comments

Comments
 (0)