const getJson = function(url){
const promise = new Promise((resolve,reject)=>{
const handler = function(){
if(this.readyState!==4){
return;
}
if(this.status === 200){
resolve(this.response);
}else{
reject(new Error(this.statusText));
}
}
const client = new XMLHttpRequest()
client.open('get',url)
client.onreadystatechange = handler
client.responseType = 'json'
client.setRequestHeader('Accept','application/json')
client.send()
})
return promise
}
getJSON("/posts.json").then((json)=> {
console.log('Contents: ' + json);
}.catch((err)=>{
console.log('err: ' + err);
});