类类:APIPromise<T>

一个表示解析结果的 Promise,同时可访问 HTTP 响应。

非 2xx 响应会以 APIError 拒绝,通过 asResponse() 获取时也不例外。

继承

  • Promise<T>

类型参数

T

T

构造函数

构造函数

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

参数

responsePromise

Promise<Response>

parseResponse

(response) => Promise<T>

返回值

APIPromise<T>

重写

ts
Promise<T>.constructor

方法

asResponse()

ts
asResponse(): Promise<Response>;

解析为原始 Response,而不解析响应体。SDK 请求会在请求超时的限制内缓冲完整响应体后再移交;之后读取响应体由调用方负责。响应体归调用方所有;不要在同一个 promise 上再 await 解析后的结果。

返回值

Promise<Response>


catch()

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

仅为 Promise 的拒绝附加回调。

类型参数

TResult

TResult = never

参数

onrejected?

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

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

返回值

Promise<T | TResult>

表示回调完成情况的 Promise。

重写

ts
Promise.catch

finally()

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

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

参数

onfinally?

(() => void) | null

Promise 敲定(兑现或拒绝)时要执行的回调。

返回值

Promise<T>

表示回调完成情况的 Promise。

重写

ts
Promise.finally

map()

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

对解析后的结果进行变换,并共享 HTTP 响应与单次响应体解析。

类型参数

U

U

参数

fn

(data) => U

返回值

APIPromise<U>


then()

ts
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。

重写

ts
Promise.then

withResponse()

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

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

返回值

Promise<WithResponse<T>>