Skip to content

Instantly share code, notes, and snippets.

@ykrkn
Created August 31, 2017 15:31
Show Gist options
  • Save ykrkn/dd561ca410cb1bfb41e6d73b68e85ffa to your computer and use it in GitHub Desktop.
Save ykrkn/dd561ca410cb1bfb41e6d73b68e85ffa to your computer and use it in GitHub Desktop.
es6 async await fetch
class API {
constructor(url){
this.BASE_URL = url;
}
async post(url, data) {
const response = await fetch(this.BASE_URL + url, {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
});
if (!(response.status >= 200 && response.status < 300)) {
console.log(response.statusText);
return null;
}
const json = await response.json();
return json;
}
}
const api = new API('http://localhost:8192');
class Z {
constructor() {
this.init();
window.zzz = this;
}
async init () {
this.id = await api.post('/batch/wizard/', {action:"CREATE"});
}
}
const z = new Z();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment