分叉汇合
spawn 分叉,wait 汇合
function* loadPage() {
const header = yield* spawn(function* loadHeader() {
yield* sleep(1000);
return "header";
});
const sidebar = yield* spawn(function* loadSidebar() {
yield* sleep(2000);
return "sidebar";
});
return {
header: yield* wait(header),
sidebar: yield* wait(sidebar),
};
} loadPage spawn 出 loadHeader 和 loadSidebar 这两个并行 process。
loadPage 等待每个 spawned process,然后返回组合后的页面数据。