2

I have 3 functions. And I want to call the other function after a function is completed. In my first function, I am setting a variable(students) in the page. And the second function uses that variable. So I am using promise. But I tried a lot of ways. But I can't execute the code with promise. I want, in my buttonClick, my three functions should work orderly async. I want like below. How can I do?

  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);
    }
  }

6 Answers 6

4

Try this way.

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); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

It didn't work @SachinShah. It worked by this order: buttonClick=>secondFunction=> thirdFunction =>buttonClickResult. But i want buttonClick => buttonClickResult.=>secondFunction=> thirdFunction.
@HasanOzdemir I have update my answer as per your flow. Please check it.
Thanks for your reply @SachinShah . But this will not work because of function not call resolve function. I added working answer
@HasanOzdemir As you say , you need to pass resolve, So I have update my answer and I have set it in only one function. So based on that you can set in other functions too.
1
students: [];
studentInfoList: [];
otherList: [];

buttonClick() {
    this.myFirstOperation()
        .then(() => {
            this.mySecondOperation();
            this.myThirdOperation();
        })
}


myFirstOperation() {
    new Promise((resolve, reject) => {
        this.studentService.getStudents().subscribe(
            result => {
                this.students = result.data;
                resolve();
            });
    })

}

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);
    }
}

3 Comments

Thanks for your reply @KrutiChoksiPatel. But this will not work because of function not call resolve function. I added working answer.
Yes you are right I will edit it thanks. In your answer mySecondOperation have promise but it not required as it is simple for loop. No need of promise.
Yes. I wrote third function only for example. I wrote for someone who looking for three async function. You can assume second function also like async.
0

You can use 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);
    }
  }

1 Comment

Thanks for your reply. But, my application is used by a lot of people. So, some of them can have old browser. And async/await doesn't work on old browser. So, i want to use promise because of working on both old and new browser. @tano
0

mySecondOperation and myThirdOperation don't do anything async, so you can just write

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);
              }
          });
}

2 Comments

Please suppose that second and third function async. I gave only example. @sloth
@HasanOzdemir then please edit your question. To those function return something, like an Observable or Promise? Do they need the data gathered in the previous methods?
0

I found the solution. @KrutiChoksiPatel 's and @SachinShah 's answer is very near to my answer. But they won't work. Because in that answer, there is no "resolve()" function like below.

 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);
    }
  }

Comments

0

Maybe you need to create 2 Promises. Try this

  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);
    }

}

1 Comment

I found solution and added. Thanks for your reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.