Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angular中等待两个呼叫完成_Angular_Typescript_Subscribe - Fatal编程技术网

如何在Angular中等待两个呼叫完成

如何在Angular中等待两个呼叫完成,angular,typescript,subscribe,Angular,Typescript,Subscribe,我有一个我想运行的函数。问题是它没有等待两个调用完成并实例化rule1和rule2,所以我得到一个错误,rule1.optionhead是未定义的。我尝试将代码嵌套到每个订阅中,但是创建执行的次数超出了我的预期。我能做些什么来修复它 澄清一下,, 我使用这个for循环是因为selectedScenario是一个ID数组,这些ID以输入值的形式存在 您应该为此使用此代码 removeAll() { Promise.all([ this.storage.remove(key1),

我有一个我想运行的函数。问题是它没有等待两个调用完成并实例化rule1和rule2,所以我得到一个错误,rule1.optionhead是未定义的。我尝试将代码嵌套到每个订阅中,但是创建执行的次数超出了我的预期。我能做些什么来修复它

澄清一下,, 我使用这个for循环是因为selectedScenario是一个ID数组,这些ID以输入值的形式存在


您应该为此使用此代码

removeAll() {
  Promise.all([
    this.storage.remove(key1),
    this.storage.remove(key2),
    this.storage.remove(key3),
  ]).then(value => doSomething());
试试ForkJoin

  Observable.forkJoin([
                this.ruleService.find(pair[0]),
                this.ruleService.find(pair[1])])
                .subscribe(
    (result: any[]) =>{ 
           this.rule1 = result[0]; 
           this.rule1 = result[1]; 

        ..Your code

    }
    );

你实际上并不是在等待任何一个,更不用说两个了。我建议您阅读RxJS,以及一般的异步。您可以使用RxJS运算符CombineTest或forkJoin@BoradAkash那么,你建议使用forkJoin,在订阅中,我将粘贴所有其他代码?如果你要发布并回答,你应该让它与OP相关。你在这里发布的内容似乎与上面的问题无关。它也没有任何解释说明您使用此代码解决了什么问题。
  Observable.forkJoin([
                this.ruleService.find(pair[0]),
                this.ruleService.find(pair[1])])
                .subscribe(
    (result: any[]) =>{ 
           this.rule1 = result[0]; 
           this.rule1 = result[1]; 

        ..Your code

    }
    );