TS TypeSafe 文档中文版 原文 ↗

类:APIPromise<T>

一个针对解析后结果的 promise,同时可访问 HTTP 响应。

非 2xx 响应会以 APIError 拒绝,包括经由 asResponse() 时。

继承自

  • Promise<T>

类型参数

T

T

构造函数

构造函数

typescript
new APIPromise<T>(responsePromise, parseResponse): APIPromise<T>;
 

参数

responsePromise

Promise<Response>

parseResponse

(response) => Promise<T>

返回值

APIPromise<T>

重写

typescript
Promise<T>.constructor
 

方法

asResponse()

typescript
asResponse(): Promise<Response>;
 

在不解析响应体的情况下解析为原始的 Response。SDK 请求会在请求超时时间内缓冲完整的 响应体,然后才移交;此后再读取该响应体由调用方负责。 调用方拥有该响应体;不要在同一 promise 上同时 await 解析后的结果。

返回值

Promise<Response>


catch()

typescript
catch<TResult>(onrejected?): Promise<T | TResult>;
 

为 Promise 的拒绝附加一个回调。

类型参数

TResult

TResult = never

参数

onrejected?

((reason) => TResult | PromiseLike<TResult>) | null

Promise 被拒绝时要执行的回调。

返回值

Promise<T | TResult>

一个 Promise,用于该回调的完成。

重写

typescript
Promise.catch
 

finally()

typescript
finally(onfinally?): Promise<T>;
 

附加一个在 Promise 敲定(已完成或已拒绝)时调用的回调。已解析的值无法从该回调中修改。

参数

onfinally?

(() => void) | null

Promise 敲定(已完成或已拒绝)时要执行的回调。

返回值

Promise<T>

一个 Promise,用于该回调的完成。

重写

typescript
Promise.finally
 

map()

typescript
map<U>(fn): APIPromise<U>;
 

转换解析后的结果,共享同一个 HTTP 响应和一次响应体解析。

类型参数

U

U

参数

fn

(data) => U

返回值

APIPromise<U>


then()

typescript
then<TResult1, TResult2>(onfulfilled?, onrejected?): Promise<TResult1 | TResult2>;
 

为 Promise 的解析和/或拒绝附加回调。

类型参数

TResult1

TResult1 = T

TResult2

TResult2 = never

参数

onfulfilled?

((value) => TResult1 | PromiseLike<TResult1>) | null

Promise 被解析时要执行的回调。

onrejected?

((reason) => TResult2 | PromiseLike<TResult2>) | null

Promise 被拒绝时要执行的回调。

返回值

Promise<TResult1 | TResult2>

一个 Promise,用于所执行的那个回调的完成。

重写

typescript
Promise.then
 

withResponse()

typescript
withResponse(): Promise<WithResponse<T>>;
 

返回解析后的结果、HTTP 响应以及请求 ID。

返回值

Promise<WithResponse<T>>