Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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 通过Promise将同步和异步混合在一起。这样就不起作用了_Javascript_Node.js_Angular_Typescript - Fatal编程技术网

Javascript 通过Promise将同步和异步混合在一起。这样就不起作用了

Javascript 通过Promise将同步和异步混合在一起。这样就不起作用了,javascript,node.js,angular,typescript,Javascript,Node.js,Angular,Typescript,这是我的密码: private loadingData() { var promise = new Promise<any>((resolve) => { resolve(); }); promise.then(() => { this.asyncServiceCall(); }) .then(() => this.syncFunctionThree()) }; 好的,让我们看看functionOn

这是我的密码:

private loadingData() {
   var promise = new Promise<any>((resolve) => 
   {
       resolve();
   });
   promise.then(() => {
       this.asyncServiceCall();
   })
   .then(() => this.syncFunctionThree())
};
好的,让我们看看
functionOne
functionTwo
。他们都在回报承诺

private functionOne() {
   return new Promise((resolve) => {
    this.user['load'] = true;
    this.service['user'].get().subscribe(d => 
  {
     this.user['d'] = d;
  },
  error => console.error(error),
  () => {
     this.role['load']=false;
   });
  resolve();
});
}

private functionTwo() {
   return new Promise((resolve) => {
    this.service['name'].get().subscribe(d => 
  {
     this.name['d'] = d;
  },
  resolve();
});
}
第三种方法
syncFunctionThree
将使用数据
this.user['d']
this.name['d']
来拥有一些业务逻辑。所以我想先调用
functionOne
functionTwo
,然后调用
syncFunctionThree

顺便说一句,我不走运。我发现 在异步方法之前调用了
syncFunctionThree


所以帮帮我。

你在
then()
中缺少了一个重要的承诺。。。您需要
返回那些承诺,否则
then()
将立即解析并转到链中的下一个then()。这就是为什么functionThree在异步承诺函数解析之前触发

private loadingData() {
   var promise = new Promise<any>((resolve) => 
   {
       resolve();
   });
   promise.then(() => {
      // return the promise this function returns
      return this.asyncServiceCall();
     // ^^^^
   })
    // this then() won't fire until asyncServiceCall resolves in previous then()
   .then((resultFromPriorThen) => this.syncFunctionThree())
}
现在用同样的方法修复
asyncServiceCall()

private asyncServiceCall(): Promise<any> {   
   return this.functionOne().then(()=>this.functionTwo());   
}
private asyncServiceCall():承诺{
返回此.functionOne(),然后(()=>this.functionTwo());
}


最后一点注意:需要在
加载数据()
中添加一个
catch()
,以防其中一个异步操作出现问题

您在
then()
中缺少调用承诺的重要部分。。。您需要
返回那些承诺,否则
then()
将立即解析并转到链中的下一个then()。这就是为什么functionThree在异步承诺函数解析之前触发

private loadingData() {
   var promise = new Promise<any>((resolve) => 
   {
       resolve();
   });
   promise.then(() => {
      // return the promise this function returns
      return this.asyncServiceCall();
     // ^^^^
   })
    // this then() won't fire until asyncServiceCall resolves in previous then()
   .then((resultFromPriorThen) => this.syncFunctionThree())
}
private functionOne() {
   return new Promise((resolve) => {
    this.service['user'].get().subscribe(resolve);
  });
}

private functionTwo() {
   return new Promise((resolve) => {
    this.service['name'].get().subscribe(resolve);
  });
}

private loadingData() {
   Promise.all([functionOne(), functionTwo()]).then(([user, name]) => {
      this.user['d'] = user;
      this.name['d'] = name;
   }).then(() => this.syncFunctionThree())
};
现在用同样的方法修复
asyncServiceCall()

private asyncServiceCall(): Promise<any> {   
   return this.functionOne().then(()=>this.functionTwo());   
}
private asyncServiceCall():承诺{
返回此.functionOne(),然后(()=>this.functionTwo());
}

最后一点注意:需要在
loadingData()
中添加一个
catch()
,以防其中一个异步操作出现问题

private functionOne() {
   return new Promise((resolve) => {
    this.service['user'].get().subscribe(resolve);
  });
}

private functionTwo() {
   return new Promise((resolve) => {
    this.service['name'].get().subscribe(resolve);
  });
}

private loadingData() {
   Promise.all([functionOne(), functionTwo()]).then(([user, name]) => {
      this.user['d'] = user;
      this.name['d'] = name;
   }).then(() => this.syncFunctionThree())
};
如果
this.service['user'].get()
是可观察的rx,则通过查看方法签名。您可以使用
this.service['user'].get().toPromise()
直接获得承诺。如果
this.service['user'].get()
有多个值,请尝试
this.service['user'].get().pipe(first())


如果
this.service['user'].get()
是可观察的rx,则通过查看方法签名。您可以使用
this.service['user'].get().toPromise()
直接获得承诺。如果
this.service['user'].get()
有多个值,请尝试
this.service['user'].get().pipe(first())
而不是
返回此.functionOne()。然后(this.functionTwo)
返回此.functionOne()。然后(this.functionTwo())
?我们是否需要在
functionTwo
的末尾添加
()
?@Bigeyes您有两个选择:
返回this.functionOne()。然后(this.functionTwo.bind(this))
返回this.functionOne()。然后(()=>this.functionTwo())
。您的版本无法按预期工作,但我想可能有问题……在
private asyncServiceCall()
this.functionOne()。然后(()=>this.functionTwo());}显示为灰色。编译器抱怨检测到无法访问的代码。@Bigeyes必须在其他函数的某个地方无法访问,直到无法工作为止。第三个函数总是在其他函数之前运行。也许我也需要将其更改为promise
返回此.functionOne()。然后(此.functionTwo)
返回此.functionOne()。然后(此.functionTwo())
?我们是否需要在
functionTwo
的末尾添加
()
?@Bigeyes您有两个选择:
返回this.functionOne()。然后(this.functionTwo.bind(this))
返回this.functionOne()。然后(()=>this.functionTwo())
。您的版本无法按预期工作,但我想可能有问题……在
private asyncServiceCall()
this.functionOne()。然后(()=>this.functionTwo());}显示为灰色。编译器抱怨检测到无法访问的代码。@Bigeyes必须在其他函数的某个地方无法访问,直到无法工作为止。第三个函数总是在其他函数之前运行。也许我也需要改变它