簡體   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