Skip to content

Commit 3ab4f2b

Browse files
committed
async event handler in React
1 parent c2104dc commit 3ab4f2b

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

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,80 @@
1+
#### 1st Basic Implementation
2+
3+
```js
4+
handleForm = async event => {
5+
this.setState({ isProcessing: true });
6+
const response = await client.sendApiRequest({
7+
value1: event.target.elements.field1.value,
8+
value2: event.target.elements.field2.value
9+
});
10+
if (response.ok) this.setState({ isProcessing: false });
11+
};
12+
```
13+
14+
the code above is totally fine. The syntactic sugar of async/await is backed by Promises which means we could also look at our code as doing something like this:
15+
16+
```js
17+
handleForm = event => {
18+
this.setState({ isProcessing: true });
19+
client.sendApiRequest({
20+
value1: event.target.elements.field1.value,
21+
value2: event.target.elements.field2.value,
22+
}).then(response => {
23+
if (response.ok)
24+
this.setState({ isProcessing: false });
25+
});
26+
});
27+
}
28+
```
29+
30+
The first part of that promise is going to execute in what is essentially a synchronous manner. That means we are safe to pass the event values as arguments.
31+
32+
#### However, it’s important to recognize that this does not mean that accessing the event values anywhere in an async function is okay. Take for example what would happen if we needed access the event after the API request.
33+
34+
```js
35+
handleForm = async event => {
36+
this.setState({ isProcessing: true });
37+
const response = await client.sendApiRequest({
38+
value1: event.target.elements.field1.value,
39+
value2: event.target.elements.field2.value
40+
});
41+
if (response.ok) {
42+
this.setState({
43+
isProcessing: false,
44+
value1: event.target.elements.field1.value,
45+
value2: event.target.elements.field2.value
46+
});
47+
}
48+
};
49+
```
50+
51+
### Now we’re accessing the event after the await, which is like accessing it in the .then chain of a promise. We would be accessing the event asynchronously now.
52+
53+
Here’s that same event written as promises again:
54+
55+
```js
56+
handleForm = event => {
57+
return new Promise((resolve, reject) => {
58+
this.setState({ isProcessing: true });
59+
client
60+
.sendApiRequest({
61+
value1: event.target.elements.field1.value,
62+
value2: event.target.elements.field2.value
63+
})
64+
.then(response => {
65+
if (response.ok) {
66+
this.setState({
67+
isProcessing: false,
68+
value1: event.target.elements.field1.value,
69+
value2: event.target.elements.field2.value
70+
});
71+
}
72+
});
73+
});
74+
};
75+
```
76+
77+
#### Further Reading
78+
79+
1> https://medium.com/@ian.mundy/async-event-handlers-in-react-a1590ed24399
80+
2> https://medium.com/@tkssharma/writing-neat-asynchronous-node-js-code-with-promises-async-await-fa8d8b0bcd7c - Very Good

0 commit comments

Comments
 (0)