Skip to content

Commit c2104dc

Browse files
committed
synchronous fetch with async-await
1 parent 6d7df22 commit c2104dc

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed
File renamed without changes.

Javascript/sequential-execution-of-codes-React-Node-Context/Async-Event-Handler-1-both-async-await-and-with-Promise.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Transform the following code with
2+
3+
```js
4+
fetch("https://api.com/values/1")
5+
.then(response => response.json())
6+
.then(json => console.log(json));
7+
```
8+
9+
Need to wrap it inside an async function to make the async-await mechanism work.
10+
11+
```js
12+
const request = async () => {
13+
const response = await fetch("https://api.com/values/1");
14+
const json = await response.json();
15+
console.log(json);
16+
};
17+
18+
request();
19+
```
20+
21+
In the first line, we make a GET request to https://api.com/values/1. Instead of continuing to the next line, we wait for the request to finish, hence await. When it finishes, it passes the resolved value to the response variable.

0 commit comments

Comments
 (0)