Skip to content

Commit 8e687a8

Browse files
committed
sequential axios call one after another
1 parent 006120d commit 8e687a8

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#### Problem Statement - I want to fire second request based on one value returned by the first request. So, axios.all will not resolve my probelem.
2+
3+
I can use async and await
4+
5+
```js
6+
async function getData() {
7+
const firstRequest = await axios.get(`${<URL1>}`);
8+
data1 = firstRequest.data[0];
9+
if (data1){
10+
const secondRequest = await axios.get(`${<URL2>}`);
11+
data1 = secondRequest.data;
12+
}
13+
return data1;
14+
}
15+
```
16+
17+
A very importnat use case of this is, when restting password. Before resetting the password, I have to make sure, that the unique-link sent to the user is valid (Not expired and the same that was sent to him) - So, for this, in the same password-reset function, I have to first make an axios call to an API endpoint, which will validate that the link sent to the user is valid. And only if this API call returns a valid respoonse, I will fire the second API call to the endpoint to actually reset the password.
18+
19+
Extract from a PasswordReset Component production grade project below
20+
21+
```js
22+
23+
componentDidMount() {
24+
const parsed = queryString.parse(this.props.location.search);
25+
this.setState({
26+
emailFromURLParams: parsed.email,
27+
uidFromURLParams: parsed.uid
28+
});
29+
}
30+
31+
// Function to reset the password - It will fire two sequential axios request and based on first request's successful response value, will do the next axios request.
32+
passwordResetOnSubmit = async e => {
33+
e.preventDefault();
34+
35+
const {
36+
emailFromURLParams,
37+
uidFromURLParams,
38+
newResetPassword,
39+
confirmNewPassword
40+
} = this.state;
41+
42+
const firstRequest = await axios
43+
.post("/api/forgot-password/verify-uid-before-password-reset", {
44+
emailFromURLParams,
45+
uidFromURLParams
46+
})
47+
.catch(error => {
48+
if (error.response.status === 400) {
49+
this.setState({ openWrongDateRangeSnackBar: true });
50+
}
51+
});
52+
const userId = await (firstRequest && firstRequest.data);
53+
54+
if (userId && newResetPassword === confirmNewPassword) {
55+
axios
56+
.put("/api/user/password-reset", {
57+
userId,
58+
newResetPassword
59+
})
60+
.then(() => {
61+
this.setState(
62+
{
63+
openNewItemAddedConfirmSnackbar: true,
64+
newResetPassword: "",
65+
confirmNewPassword: ""
66+
},
67+
68+
() => {
69+
setInterval(function() {
70+
history.push("/login");
71+
}, 3000);
72+
}
73+
);
74+
})
75+
.catch(error => {
76+
if (error.response.status === 404) {
77+
this.setState({
78+
message: "Authentication failed. User not found."
79+
});
80+
} else if (error.response.status === 401) {
81+
this.setState({ message: "Authentication failed. Wrong Password" });
82+
} else if (
83+
error.response.status === 500 ||
84+
error.response.status === 422
85+
) {
86+
this.setState({
87+
message:
88+
"Authentication failed. Only for Port authorized personnel"
89+
});
90+
}
91+
});
92+
}
93+
};
94+
95+
```
96+
97+
#### Further Reading
98+
99+
https://stackoverflow.com/questions/47343225/making-2-sequential-requests-with-axios-second-request-depends-on-the-response

0 commit comments

Comments
 (0)