Async func
Babel + JSX
const sendRequest = async (url, type, options) => {
try {
throw Error('Something went wrong');
const response = await fetch(url, options);
return await response[type]();
} catch (error) {
return Promise.reject(error);
}
};
sendRequest('https://jsonplaceholder.typicode.com/posts/1', 'json')
.then(data => console.error(`Data: ${data}`))
.catch(error => console.error(`Error: ${error}`));