我试图使getVideos()
异步,然后在函数filterVideos
中调用并使用其axios调用的结果。但是,这些诺言给我带来了麻烦。
getVideos
getVideos() {
let myFirstPromise = new Promise((resolve) => {
const url = process.env.REACT_APP_MIDDLEWARE_URL + "/api/videolist";
const options = {
headers: {
Authorization: "Bearer " + localStorage.getItem("userToken"),
},
};
axios.get(url, options).then(
(res) => {
this.setState({
video: res.data.videos[0],
videos: res.data.videos,
});
console.log(this.state.videos);
resolve(res);
},
(error) => {
if (error.response.status === 401) {
this.props.signOut();
}
}
);
});
}
filterVideos
filterVideos() {
let completed = true;
this.getVideos().then((res) => {
res.forEach((video) => {
if (video.status != "COMPLETED") {
completed = false;
}
});
});
if (completed === false) {
this.waitForCompleted();
}
}