恢复边界
guard(...) 把 resumable(...) 的失败交给恢复处理器。
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}`];
},
);
}publishListing 进入 guard(...),reviewListing 会停在 resumable(...)。
scanPhotos 抛错时,reviewListing 正在等待。
approveManually 在 guard 边界处理这次失败。
恢复值会作为 approval 回到 reviewListing。