JSFiddle - React, Tailwind, and code Playground
JavaScript
function getAsync(url) {
return new Promise((resolve, reject) => {
var httpReq = new XMLHttpRequest();
httpReq.onreadystatechange = () => {
if (httpReq.readyState === 4) {
if (httpReq.status === 200) {
resolve(JSON.parse(httpReq.responseText));
} else {
reject(new Error(httpReq.statusText));
}
}
};
httpReq.open("GET", url, true);
httpReq.send();
});
};
getAsync('https://jsonplaceholder.typicode.com/posts/1').then((data) => {
const post = 'Title: ' + data.title + '\n\nBody: ' + data.body;
alert(post)
}).catch((err) => {
alert(err);
});