0

I need to call function only when is first function loaded ? I have choice which work ( but not enough good for me )

 allData(){ 
 }  

Do I have any better solution ?

6
  • how do you know when this.allData is finished? does it return a Promise? can it take a callback function? as is the answers are many and varied Commented Aug 20, 2021 at 8:16
  • please share implementation of allData() method Commented Aug 20, 2021 at 8:17
  • allData(){ this.accountsService.getContactPerson().subscribe( (data: any) => { console.log(data) }, (err: Error) => console.log(err) ) } Commented Aug 20, 2021 at 8:17
  • this is allData function. Finished was go to subscribe data Commented Aug 20, 2021 at 8:18
  • put it in the question where it can be read - and what to do if there's an error Commented Aug 20, 2021 at 8:18

3 Answers 3

1

allData() performs an async operation so you need to call the second function when the getContactPerson() returns a value. In your case you need to call it in the next callback. Try to move the second function call inside the callback, as following:

allData(){ 
   this.accountsService
    .getContactPerson()
    .subscribe( 
       (data: any) => { 
         console.log(data)
         /* DO SOME STUFF HERE */

         this.lastAddedItem(this.contacts, contact);

       }, 
       (err: Error) => console.log(err) 
     ) 
} 


this.allData()

UPDATE

If you want to use the same function in onInit and after another event such as a button click, you can refactor you code to return an Observable and use it in multiple places.

allData = () => this.accountsService.getContactPerson();

ngOnInit(){
   this.allData().subscribe( 
       (data: any) => { /* DO SOME STUFF HERE */ }, 
       (err: Error) => console.log(err) 
     ) 
}

refreshList = () => {
   this.allData().subscribe( 
       (data: any) => { 

         /* DO SOME OTHER STUFF HERE */

         this.lastAddedItem(this.contacts, contact);

       }, 
       (err: Error) => console.log(err) 
     ) 
}


// This is just an example for an async call
anotherAsyncCall = () => {
   this.allData()
       .pipe(
          switchMap((result) => this.contactService.makeAsyncCall())
       )
       .subscribe()
}
Sign up to request clarification or add additional context in comments.

5 Comments

I can't to do this. Because allData is loaded on init, but I need this call only on add new item in array...
I'm not sure I understand you correctly. What do you mean by "allData is loaded on init"? What is the flow of your program?
allData is onInit function. I call allData also to refresh list.. I this case i want to refresh list and call lastAddedItem ... I wan't onInit to also call lastAddedItem
@fipodas I updated the answer trying to include what you need as far as I understand. Hope this helps.
I did not explain well unfortunately... i need to change question
0

This is a classic case for using switchMap operator. First of all, do not subscribe in allData() method, instead just return the observable:

allData() { 
    return this.accountsService.getContactPerson();
} 

then, use switchMap where you are calling allData():

ngOnInit() {
    this.allData().pipe(
        switchMap(data => {
            // Do what you want to with the data. 
            // I am assuming that the data is either assigned to contacts 
            // or contact ... but I am not sure. 

            // Return if this method also returns an observable.
            return this.lastAddedItem(this.contacts, contact);

            // otherwise comment the above line & uncomment the below line:
            // this.lastAddedItem(this.contacts, contact);
        }
    ).subscribe( ... );

Comments

0

I believe that you are getting observable from this this.accountsService.getContactPerson() method, so I will suggest you please try this approach.

async ngOnInit() {
   await this.loadAllData();
   this.lastAddedItem(this.contacts, contact);
}

public async loadAllData() {
    try {
        let allDataResponse$ = this.accountsService.getContactPerson();
        let allData = await lastValueFrom(allDataResponse$);
    
    } catch(e) {
       console.log(e);
    }
}

3 Comments

allData is onInit function. I call allData also to refresh list.. I this case i want to refresh list and call lastAddedItem ... I wan't onInit to also call lastAddedItem
Alright! I modify my answer, please have a look, it may help you
I did not explain well unfortunately... i need to change question

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.