繁体   English   中英

如何调用promise函数链?

[英]How to call chain of promise functions?

我需要发出一连串的承诺:

main(): Promise<any> {
  1) call get();
  2) then `get()` is finished call getTwo()
  3) When `getTwo()` is finished return promise to main() function
}

get(): Promise<any>  {
  //
}

getTwo(): Promise<any>  {
  //
}

我试图证明我需要做什么。

这就是承诺链的运行方式:

return this.get()
.then(data1 => {
    return this.getTwo(data1);
}).then(data2 => {
    return data2;
})

有关更多详细信息, 请阅读

较短的版本可能是@JoeClay的评论

this.get().then(this.getTwo)

您似乎在寻找

main(): Promise<any> {
  return get().then(getTwo);
}

请注意,当getTwo完成时,它不会“将诺言返回给main()函数”,而是解决了then创建并立即返回的诺言。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM