用async & await代替promise
library.js
//Easy http3 Library
//加入 fetch + arrow+class+async
class EasyHTTP{
//Make HTTP get request
async get(url){
const response = await fetch(url);
const resData = await response.json();
return resData;
}
//Make Http POST request
async post(url,data){
const response =await fetch(url,{
method:"POST",
headers:{
'Content-type':'application/json',
},
body:JSON.stringify(data)
});
const resData = await response.json();
return resData;
}
//Make an HTTP PUT request
async put(url,data){
const response = await fetch(url,{
method:"PUT",
headers:{
'Content-type':'application/json',
},
body:JSON.stringify(data) //传入时要变为string
});
const resData = await response.json();
return resData;
}
//MAKE an HTTP delete request
async delete(url){
const response = await fetch(url,{
method:"DELETE",
headers:{
'Content-type':'application/json',
}
});
const resData = await `resource deleted`;
return resData;
}
}