xxxxxxxxxx
// way 1
const getData = async (url) => {
const response = await fetch(url)
const json = await response.json()
return json
}
const url = `https://jsonplaceholder.typicode.com/posts?userId=3`
try {
const data = await getData(url)
console.log(data)
} catch (error) {
console.log(error.message)
}
/* ------------ Way 2 --------------- */
const getData = (url) =>
new Promise((resolve, reject) => {
fetch(url)
.then(response => response.json())
.then(json => resolve(json))
.catch(error => reject(error))
})
const url = `https://jsonplaceholder.typicode.com/todos?completed=true&userId=2`
getData(url)
.then(data => console.log(data))
.catch(error => console.log(error.message))
xxxxxxxxxx
const [advice, setAdvice] = useState("");
useEffect(() => {
const url = "https://api.adviceslip.com/advice";
// Create async function
const fetchData = async () => {
try {
const response = await fetch(url);
const jsonData = await response.json();
console.log(jsonData.slip.advice);
setAdvice(jsonData.slip.advice);
} catch (error) {
console.log("error", error);
}
};
// Call fetch function
fetchData();
}, []);
xxxxxxxxxx
fetch('https://jsonplaceholder.typicode.com/todos/1') // url link বসাবে
.then(response => response.json())
.then(data => console.log(data))
xxxxxxxxxx
fetch('https://api.myjson.com/bins/1h3vb3', {method: 'get'})
.then(response => response.json())
.then(data => console.log(data));