Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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
Javascript 嵌套承诺结构顺序_Javascript_Angular_Loops_Typescript_Promise - Fatal编程技术网

Javascript 嵌套承诺结构顺序

Javascript 嵌套承诺结构顺序,javascript,angular,loops,typescript,promise,Javascript,Angular,Loops,Typescript,Promise,我正在努力实现一些有点棘手的事情,因为我还不习惯承诺 我想填写 我有两点: 一个得到所有用户 另一个获取给定Id的ObjectInfo 这就是我所拥有的: const result: Map<string, String[]> = new Map(); return this.http.get(this.adress + '/', this.options).toPromise() .then((response) => { for (const id of r

我正在努力实现一些有点棘手的事情,因为我还不习惯承诺

我想填写

我有两点:

  • 一个得到所有用户
  • 另一个获取给定Id的ObjectInfo
这就是我所拥有的:

const result: Map<string, String[]> = new Map();
return this.http.get(this.adress + '/', this.options).toPromise()
  .then((response) => {
    for (const id of response.json()){
      this.http.get(this.adress + '/' + id + '/properties').toPromise()
      .then((rep) => {
        result.set(id, rep.json());
        console.log('#1: ', result);
      }).catch(this.handleError)
    }
  })
  .then(() => console.log('#2: ', result))
  .then(() => result)
  .catch(this.handleError);
const result:Map=newmap();
返回this.http.get(this.address+'/',this.options).toPromise()
。然后((响应)=>{
for(response.json()的常量id){
this.http.get(this.address+'/'+id+'/properties').toPromise()
.然后((代表)=>{
set(id,rep.json());
console.log(“#1:”,结果);
}).接住(这个.把手错误)
}
})
.then(()=>console.log('#2:',result))
.然后(()=>结果)
.接住(这个.把手错误);
在这里,我希望控制台中的#1后面跟着#2,但事实并非如此。我知道我应该尝试使用
Promise.all()
,但我真的不知道如何设置它,因为我正在嵌套循环中创建承诺

这是针对Angular项目的,到目前为止,实际代码仍在运行,但会抛出ExpressionChangedTerithasBeenCheckedError。
为了避免这种情况,更改my promise函数应该可以做到这一点。

这部分代码

.then((response) => {
  // <-- HERE -->
  // this function must return a promise

  for (const id of response.json()){
    // ...
  }
})
.then(() => {
  // right now, this function executes **before** the requests
  // in the promise above are resolved
});

为什么您要保证您尝试使用可观察对象?检查后不要更改表达式欢迎使用堆栈溢出!angular标记应用于angular framework,版本2及更高版本。angularjs标记应仅用于angularjs框架,即1.x范围内的版本。请回答您的问题以删除您未使用的版本的标记。由于这不是纯Javascript,请使用您正在使用的语言对其进行标记。嵌套循环?我看不出来。没关系。你所需要做的就是从你的
回调返回一个承诺,然后在其中循环。谢谢你的帮助,这正是我想要的!还是要换表情吗?我会找到答案的
.then((response) => {
  return Promise.all(
    Array
      .from(response.json())
      .map(id => {
        return this.http.get(this.adress + '/' + id + '/properties')
          .toPromise()
          .then((rep) => {
            result.set(id, rep.json());
            console.log('#1: ', result);
          })
          .catch(this.handleError)
      })
  );
})
.then(...)