Promise Typescriptから値を取得する 質問する

Promise Typescriptから値を取得する 質問する

Typescript クラス内の関数の 1 つが を返しますPromise<string>。その promise 内の値を unwrap/yield するにはどうすればよいでしょうか。

functionA(): Promise<string> {
   // api call returns Promise<string>
}

functionB(): string {
   return this.functionA() // how to unwrap the value inside this  promise
}

ベストアンサー1

そのプロミス内の値をアンラップ/生成するにはどうすればいいでしょうか

async/でそれを行うことができますawait。async から sync に移行しただけだと誤解しないでください。async await は の単なるラッパーです.then

functionA(): Promise<string> {
   // api call returns Promise<string>
}

async functionB(): Promise<string> {
   const value = await this.functionA() // how to unwrap the value inside this  promise
   return value;
}

さらに遠く

おすすめ記事