thenIf<R> method

Future<Response<R>> thenIf<R>(
  1. bool condition(
    1. Response<T> prevResponse
    ),
  2. Future<Response<R>> nextRequest(
    1. Response<T> prevResponse
    )
)

条件链式调用(根据前一个请求的结果决定是否执行下一个请求)

示例:

final result = await http.send(...)
  .thenIf(
    (prevResponse) => prevResponse.extractField<bool>('needNextStep') == true,
    (prevResponse) => http.send(method: hm.post, path: '/next-step'),
  );

Implementation

Future<Response<R>> thenIf<R>(
  bool Function(Response<T> prevResponse) condition,
  Future<Response<R>> Function(Response<T> prevResponse) nextRequest,
) async {
  final prevResponse = await this;
  if (!prevResponse.isSuccess || !condition(prevResponse)) {
    return prevResponse as Response<R>;
  }
  return await nextRequest(prevResponse);
}