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

View File

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

View File

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

View File

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