[Fixed] Wait for response in angular subscriber

Issue

I have seen multiple posts with the same title but none of them is what I need for my use case.
I have an array of items, item property can have (‘serials’,’macs’,’null’) I have to call a different API end point for each one
and for example inside ‘model’ there are an array of models
which also need to be sent one by one. I know it’s not the best way to deal with this but it’s a long story and I’m not responsible for this mess of a design .
the service code :

  createDevice(data: Device) {
    return this.http.post('somewhere', data, {
      headers: this.httpHeader,
    });
  }

component code:

 this.sourceDataSet.forEach(item=>{

 if(item.serials){
   item.serials.forEach(serial=>{
  //create it

  })
  
  }
 else if(item.macs){

  item.macs.forEach(serial=>{
  //create it

  })

  }
else{

  // create it 

  } 
})

the problem is there are other properties which will get affected by this and I don’t want them to.
is there a way to do this.

Solution

you can use toPromise() to convert it into a promise and use await.

https://www.learnrxjs.io/learn-rxjs/operators/utility/topromise

Leave a Reply

(*) Required, Your email will not be published