0

I'm performing multiple task and each task is dependent on previous task execution. So in my example what I want is after getting all the Id, i should get their respective blob value and then finish the execution by storing it in a variable. I'm very new to javascript and angular, please help me out. Here's what I'm trying

//this method will get the response from the rest api
async getIDFromAssets(){
    this.blobDataArray=[];
    this.service.getAssetsData().subscribe(async (res: JSON) => {
//after getting the response I'm filtering through it to get sepcific Id using this.getFileId() method
       this.getFileId(res).then((data)=>{

                console.log("blob "+data)
       })
    })
}

//below method will get one Id at a time and will call another method to get it's blob value 
async getFileId(res){
        this.fileId = [];
        Object.keys(res).forEach(keys => {
            if (keys == 'emb') {
                let responseValue = res[keys];
                Object.keys(responseValue).forEach(async (keys1) => {
                    if (keys1 === 'file') {
                        let responseArray = responseValue[keys1];
                        for (let file of responseArray) {
                            let temp: string = file.metadata.contentType;
                            if (temp.startsWith('image')) {
//Here I'm getting id value 'file._id' and using that I'm calling another method 'getBlobData()' to get its blob value
                               let data=await this.getBlobData(file._id);

                                this.blobDataArray.push(data);
                            }
                        }
                        return this.blobDataArray

                    }
                });
            }
        });
    }
// method to get the blob value
    async getBlobData(fileId){
        this.articleDetailService.getBlobDataFromAssets(fileId).subscribe(async (res)=>{

            let imageObj={
                'id':fileId,
                'blob':res
            }

            return imageObj;
        })
    }

1 Answers1

0

You need to use RxJs to avoid the nested subscription to chain your calls, possible methods to use are mergeMap and filter

Please take a look at this answer here.

Furqan S. Mahmoud
  • 1,417
  • 2
  • 13
  • 26