-
Notifications
You must be signed in to change notification settings - Fork 2
/
PromisesAndAxios.js
81 lines (72 loc) · 2.27 KB
/
PromisesAndAxios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import axios from 'axios';
import React, { useEffect, useState } from 'react';
export default function PromisesAndAxios() {
const [apiData, setapiData] = useState([]);
const [postData, setpostData] = useState([]);
const [searchWord, setsearchWord] = useState(573201);
function fetchDetails() {
let anime = new Promise((resolve, reject) => {
axios.get("https://api.jikan.moe/v4/anime")
.then((res) => {
setapiData(res.data.data);
})
if (resolve) {
resolve();
} else if (reject) {
reject();
}
})
return anime;
}
function fetchPin() {
var pin = new Promise((resolve, reject) => {
setTimeout(() => {
axios.get("https://api.postalpincode.in/pincode/" + searchWord)
.then((res) => {
setpostData(res.data[0].PostOffice);
}).catch((err) => console.log(err.message))
if (resolve) {
resolve();
} else if (reject) {
reject();
}
}, 5000)
})
return pin;
}
let allPromisea = () => {
// let anime = fetchDetails();
// let pin = fetchPin();
Promise.all([fetchDetails(), fetchPin()])
// Promise.all([anime, pin])
.then(() => { printResolved() })
}
let printResolved = () => {
window.alert("Everything is resolved")
}
useEffect(() => {
allPromisea();
}, []);
return (
<div>
<div>
{postData.map((datas) => {
return (
<h1>{datas.Name}</h1>
)
})
}
</div>
<div>
{apiData.map((datas) => {
return (
<h3>{datas.title}</h3>
)
})
}
</div>
<input type="text" value={searchWord} onChange={(e) => setsearchWord(e.target.value)} />
<h2>{searchWord}</h2>
</div>
);
}