fetch with promise javascript code example
Example 1: javascript fetch api get
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
Example 2: fetch
var formData = new FormData();
var fileField = document.querySelector("input[type='file']");
formData.append('username', 'abc123');
formData.append('avatar', fileField.files[0]);
fetch('https://example.com/profile/avatar', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
Example 3: javascript fetch get data from promise
var x = fetch(SOME_URL, SOME_POST_DATA)
.then((response) => response.json())
.then((responseJSON) => {
console.log(responseJSON);
});