shajara图解Scope 拥有的 Work
中文

Scope 拥有的 Work

child-scope work 收束

branch(commitArticle)spawn(updateSearchIndex)owned processbranch waits for child scopepublishArticlependingupdateSearchIndexpendingcommitArticlepending
function* publishArticle() {
  const result = yield* branch(function* commitArticle() {
    yield* spawn(function* updateSearchIndex() {
      yield* sleep(1000);
    });

    return "published";
  });

  return result;
}
publishArticle 进入 branch 运行 commitArticle,随后返回它的结果。
branch 创建一个 child scope,边界内 spawn 出来的工作归 child scope 拥有。
commitArticle 在 updateSearchIndex 完成前先产生 published。
branch 会等 scope-owned work 收束后再返回。