Rjk*_*Rjk 4 javascript promise typescript es6-promise
Typescript类中的一个函数返回一个Promise<string>。我该如何实现承诺中的价值?
functionA(): Promise<string> {
// api call returns Promise<string>
}
functionB(): string {
return this.functionA() // how to unwrap the value inside this promise
}
Run Code Online (Sandbox Code Playgroud)
尝试这个
functionB(): string {
return this.functionA().then(value => ... );
}
Run Code Online (Sandbox Code Playgroud)
我如何解开/产生承诺中的价值
您可以使用async/ 来做到这一点await:https : //basarat.gitbooks.io/typescript/content/docs/async-await.html
不要愚蠢地认为您刚刚从异步转到了同步,它只是包装.then:https : //basarat.gitbooks.io/typescript/content/docs/async-await.html#genic-JavaScript