【发布时间】:2016-12-26 05:15:06
【问题描述】:
打字稿类中的一个函数返回Promise<string>。我如何解开/产生该承诺中的价值。
functionA(): Promise<string> {
// api call returns Promise<string>
}
functionB(): string {
return this.functionA() // how to unwrap the value inside this promise
}
【问题讨论】:
-
你不能直接从 Promise 中获取值。您在 promise 上使用
.then()处理程序来访问该值。 -
这根本不可能,你买的东西还没发货就不能玩了,即使你有发货通知也不行;或者商店的人向您保证包裹正在运送中。 Promise 管理时间,你想要的字符串还不存在,除了
then()之外,没有办法知道它什么时候会出现。functionB()必须返回一个 Promise,这是没有办法的。
标签: javascript typescript promise es6-promise