Created
August 31, 2017 15:31
-
-
Save ykrkn/dd561ca410cb1bfb41e6d73b68e85ffa to your computer and use it in GitHub Desktop.
es6 async await fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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