【问题标题】:How to synchronise my code execution in angular如何以角度同步我的代码执行
【发布时间】:2020-05-22 17:41:06
【问题描述】:

我正在执行多个任务,每个任务都依赖于之前的任务执行。因此,在我的示例中,我想要的是在获取所有 Id 之后,我应该获取它们各自的 blob 值,然后通过将其存储在变量中来完成执行。我对javascript和角度很陌生,请帮帮我。这就是我正在尝试的方法

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

【问题讨论】:

    标签: javascript angular asynchronous promise async-await


    【解决方案1】:

    您需要使用RxJs 来避免嵌套订阅链接您的调用,可能的使用方法是mergeMap 和@ 987654323@

    请在此处查看answer。

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      相关资源
      最近更新 更多