convert fetch usage to async/await

This commit is contained in:
Simone Gotti 2019-03-29 18:19:07 +01:00
parent ef5a9c4994
commit 219b2d2e2f
4 changed files with 35 additions and 46 deletions

View File

@ -36,7 +36,7 @@ export default {
}; };
}, },
methods: { methods: {
fetch: function() { fetch() {
if (this.fetching) { if (this.fetching) {
return; return;
} }
@ -49,7 +49,7 @@ export default {
this.getLogs(); this.getLogs();
} }
}, },
streamLogs: function() { streamLogs() {
let path = "/logs?runID=" + this.runid + "&taskID=" + this.taskid; let path = "/logs?runID=" + this.runid + "&taskID=" + this.taskid;
if (this.setup) { if (this.setup) {
path += "&setup"; path += "&setup";
@ -69,14 +69,14 @@ export default {
this.es.close(); this.es.close();
}; };
}, },
getLogs: function() { async getLogs() {
let path = "/logs?runID=" + this.runid + "&taskID=" + this.taskid; let path = "/logs?runID=" + this.runid + "&taskID=" + this.taskid;
if (this.setup) { if (this.setup) {
path += "&setup"; path += "&setup";
} else { } else {
path += "&step=" + this.step; path += "&step=" + this.step;
} }
fetch(apiurl(path)) let res = await fetch(apiurl(path))
.then(r => { .then(r => {
if (r.status == 200) { if (r.status == 200) {
return r.text(); return r.text();

View File

@ -38,17 +38,14 @@ export default {
}; };
} }
}, },
fetchProjects(ownertype, ownername) { async fetchProjects(ownertype, ownername) {
let path = let path =
"/projectgroups/" + encodeURIComponent(ownertype + "/" + ownername); "/projectgroups/" + encodeURIComponent(ownertype + "/" + ownername);
path += "/projects"; path += "/projects";
fetch(apiurl(path)) let res = await (await fetch(apiurl(path))).json();
.then(res => res.json())
.then(res => {
console.log(res); console.log(res);
this.projects = res; this.projects = res;
console.log("projects", this.projects); console.log("projects", this.projects);
});
} }
}, },
created: function() { created: function() {

View File

@ -134,34 +134,32 @@ export default {
if (task.status == "running") return "running"; if (task.status == "running") return "running";
return "unknown"; return "unknown";
}, },
restartRun(run, fromStart) { async restartRun(run, fromStart) {
fetch(apiurl("/run/" + run.id + "/actions"), { let res = await fetch(apiurl("/run/" + run.id + "/actions"), {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
action_type: "restart", action_type: "restart",
from_start: fromStart from_start: fromStart
}) })
}).then(r => { });
console.log("r: " + r); console.log("r: " + r);
if (r.status == 200) { if (r.status == 200) {
return r.json(); return r.json();
} }
throw Error(r.statusText); throw Error(r.statusText);
});
}, },
stopRun(run) { async stopRun(run) {
fetch(apiurl("/run/" + run.id + "/actions"), { let res = fetch(apiurl("/run/" + run.id + "/actions"), {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
action_type: "stop" action_type: "stop"
}) })
}).then(r => { });
console.log("r: " + r); console.log("r: " + r);
if (r.status == 200) { if (r.status == 200) {
return r.json(); return r.json();
} }
throw Error(r.statusText); throw Error(r.statusText);
});
} }
} }
}; };

View File

@ -119,32 +119,26 @@ export default {
} }
this.pollData(); this.pollData();
}, },
fetchProject() { async fetchProject() {
let path = let path =
"/projects/" + "/projects/" +
encodeURIComponent( encodeURIComponent(
this.ownertype + "/" + this.ownername + "/" + this.projectname this.ownertype + "/" + this.ownername + "/" + this.projectname
); );
fetch(apiurl(path)) let res = await (await fetch(apiurl(path))).json();
.then(res => res.json())
.then(res => {
console.log(res); console.log(res);
this.project = res; this.project = res;
console.log("project", this.project); console.log("project", this.project);
this.fetchRuns(); this.fetchRuns();
});
}, },
fetchUser() { async fetchUser() {
fetch(apiurl("/users/" + this.username)) let res = await (await fetch(apiurl("/users/" + this.username))).json();
.then(res => res.json())
.then(res => {
console.log(res); console.log(res);
this.user = res; this.user = res;
console.log("user", this.user); console.log("user", this.user);
this.fetchRuns(); this.fetchRuns();
});
}, },
async fetchRuns() { async fetchRuns() {
let group; let group;