is fetch a function code example
Example 1: javascript fetch api
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
});
Example 2: fetch api javascript
fetch('http://example.com/songs')
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
Example 3: fetch api template
//fetch api template
const fetchData=(inputs)=>{
fetch('https://example.com/api', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(inputs)
})
.then(function(response) {
return response.json();
})
.then(function(data) {
ChromeSamples.log(data.whatever);
});
}
Example 4: fetch api in js
// fetch API
var myData = async () => {
try {
const raw_response = await fetch("https://jsonplaceholder.typicode.com/users");
if (!raw_response.ok) { // check for the 404 errors
throw new Error(raw_response.status);
}
const json_data = await raw_response.json();
console.log(json_data);
}
catch (error) { // catch block for network errors
console.log(error);
}
}
fetchUsers();