shajara图解恢复边界
中文

恢复边界

guard 边界内的 resumable 恢复

guard(reviewListing)resumable(scanPhotos)recovery valueresumable valuepublishListingpendingscanPhotospendingreviewListingpendingapproveManuallypending
function* publishListing() {
  yield* guard(
    function* reviewListing() {
      const approval = yield* resumable(
        function* scanPhotos() {
          yield* sleep(1000);
          throw new Error("scanner offline");
        },
      );

      yield* recordApproval(approval);
    },

    function* approveManually(error) {
      yield* sleep(1000);
      return [true, `manual approval: ${error.kind}`] as const;
    },
  );
}
publishListing 进入 guard 运行 reviewListing。
reviewListing 通过 resumable 启动 scanPhotos,并等待 resumable 返回值。
scanPhotos 在 child scope 内失败后,guard 在同一边界内把这次失败交给 approveManually 处理。
approveManually 返回 approval,resumable 把这个值交回 reviewListing 的等待点。