Angular typescript中带承诺的角度调用函数
我有3个功能。我想在函数完成后调用另一个函数。在我的第一个函数中,我在页面中设置一个变量(students)。第二个函数使用这个变量。所以我用承诺。但是我尝试了很多方法。但我不能保证执行代码。我想,在我的buttonClick中,我的三个函数应该异步有序地工作。我想要下面这样的。我该怎么办Angular typescript中带承诺的角度调用函数,angular,typescript,promise,Angular,Typescript,Promise,我有3个功能。我想在函数完成后调用另一个函数。在我的第一个函数中,我在页面中设置一个变量(students)。第二个函数使用这个变量。所以我用承诺。但是我尝试了很多方法。但我不能保证执行代码。我想,在我的buttonClick中,我的三个函数应该异步有序地工作。我想要下面这样的。我该怎么办 students: []; studentInfoList: []; otherList: []; buttonClick() { const myPromise = new Pro
students: [];
studentInfoList: [];
otherList: [];
buttonClick() {
const myPromise = new Promise(function (resolve, reject) {
myFirstOperation();
});
myPromise()
.then(result => {
mySecondOperation();
})
.then(result => {
myThirdOperation();
});
}
myFirstOperation() {
this.studentService.getStudents().subscribe(
result => {
this.students = result.data;
});
}
mySecondOperation() {
for (let student of this.students) {
this.studentInfoList.push({ studenNameSurname=student.Name + student.Surname });
}
}
myThirdOperation() {
for (let studentInfo of this.studentInfoList) {
this.otherList.push(this.studentInfoList);
}
}
您可以使用async/await
students: [];
studentInfoList: [];
otherList: [];
async buttonClick() {
await this.myFirstOperation();
this.mySecondOperation();
this.myThirdOperation();
}
async myFirstOperation() {
const result = await this.studentService.getStudents().toPromise();
this.students = result.data;
}
mySecondOperation() {
for (let student of this.students) {
this.studentInfoList.push({ studenNAmeSurname=student.Name + student.Surname });
}
}
myThirdOperation() {
for (let studentInfo of this.studentInfoList) {
this.otherList.push(studentInfoList);
}
}
mySecondOperation
和myThirdOperation
不执行任何异步操作,因此您可以直接编写
buttonClick() {
this.studentService.getStudents().subscribe(
result => {
this.students = result.data;
for (let student of this.students) {
this.studentInfoList.push({ studenNameSurname=student.Name + student.Surname });
}
for (let studentInfo of this.studentInfoList) {
this.otherList.push(this.studentInfoList);
}
});
}
这样试试
student = [];
studentInfoList = [];
otherList = [];
this.buttonClick().then(() => {
this.buttonClickResult().then((data) => {
this.secondFunction().then(() => {
this.thiedFunction();
});
});
})
buttonClick(){
return new promise((resolve , reject) => {
// Do your stuff hear
})
}
buttonClickResult(){
return new promise((resolve , reject) => {
this.studentService.getStudents().subscribe((result) => {
this.student.push(...result.data);
resolve("pass data hear"); // Pass data if you need.
})
})
}
secondFunction(){
return new promise((resolve , reject) => {
for (let data of this.student) {
this.studentInfoList.push({ "studenNameSurname" : data.Name + data.Surname });
}
})
}
thiedFunction(){
this.otherList.push(...this.studentInfoList);
}
我找到了解决办法@KrutiChoksiPatel和@Sachinsah的答案与我的答案非常接近。但它们不会起作用。因为在这个答案中,没有像下面这样的“resolve()”函数
buttonClick() {
this.myFirstOperation().then(() => {
this.mySecondOperation(() => {
this.myThirdOperation();
});
});
}
myFirstOperation() {
return new Promise((resolve, reject) => {
this.studentService.getStudents().subscribe(
result => {
this.students = result.data;
resolve(); //This row is important. Because of not working
});
})
}
mySecondOperation() {
return new Promise((resolve, reject) => {
for (let student of this.students) {
this.studentInfoList.push({ studenNameSurname=student.Name + student.Surname });
}
resolve(); //This row is important. Because of not working
})
}
myThirdOperation() {
for (let studentInfo of this.studentInfoList) {
this.otherList.push(this.studentInfoList);
}
}
也许你需要做出两个承诺。试试这个
students: [];
studentInfoList: [];
otherList: [];
buttonClick() {
const myPromise = new Promise(function (resolve, reject) {
this.studentService.getStudents().subscribe(
result => {
this.students = result.data;
resolve();
});
});
myPromise()
.then(result => {
mySecondOperation();
});
}
mySecondOperation() {
const my2Promise = new Promise(function (resolve, reject) {
for (let student of this.students) {
this.studentInfoList.push({ studenNameSurname=student.Name + student.Surname });
}
resolve();
});
my2Promise()
.then(result => {
myThirdOperation();
});
}
myThirdOperation() {
for (let studentInfo of this.studentInfoList) {
this.otherList.push(this.studentInfoList);
}
}谢谢您的回复。但是,我的应用程序被很多人使用。因此,其中一些可以使用旧浏览器。async/await在旧浏览器上不起作用。所以,我想使用promise,因为它可以处理新旧浏览器@t请假定第二个和第三个函数是异步的。我只举了一个例子@sloth@HasanOzdemir然后请编辑你的问题。向这些函数返回一些东西,比如
可观察的
或承诺
?他们需要以前方法收集的数据吗?Sachinsah没有用。它按如下顺序工作:buttonClick=>secondFunction=>thirdFunction=>buttonClickResult。但是我想要buttonClick=>buttonClickResult.=>secondFunction=>thirdFunction.@HasanOzdemir我已经根据您的流程更新了我的答案。请检查一下。谢谢您的回复@sachinsah。但这将不起作用,因为函数不调用resolve函数。我加上了工作answer@HasanOzdemir正如您所说,您需要通过resolve,所以我更新了我的答案,并且只在一个函数中设置了它。基于此,您还可以设置其他功能。谢谢您的回复@KrutiChoksiPatel。但这将不起作用,因为函数不调用resolve函数。我添加了工作答案。是的,你是对的,我会编辑它谢谢。在您的回答中,mySecondOperation有承诺,但它不是必需的,因为它对于循环来说很简单。不需要承诺,是的。我只写了第三个函数作为例子。我是为寻找三个异步函数的人写的。您可以假设第二个函数也类似于async。谢谢你的回复。
students: [];
studentInfoList: [];
otherList: [];
buttonClick() {
const myPromise = new Promise(function (resolve, reject) {
this.studentService.getStudents().subscribe(
result => {
this.students = result.data;
resolve();
});
});
myPromise()
.then(result => {
mySecondOperation();
});
}
mySecondOperation() {
const my2Promise = new Promise(function (resolve, reject) {
for (let student of this.students) {
this.studentInfoList.push({ studenNameSurname=student.Name + student.Surname });
}
resolve();
});
my2Promise()
.then(result => {
myThirdOperation();
});
}
myThirdOperation() {
for (let studentInfo of this.studentInfoList) {
this.otherList.push(this.studentInfoList);
}