Promise Ҏ֎ͷԠ༻
• ྫ͑ Result ܕ͜Εʹͯ·Δ
• then() ʹରԠ͢ΔͷԼͷΑ͏ͳ݅ذ
type Result = Ok | Err;
if (!res.isOk) return res;
return cont(res.value);
Slide 47
Slide 47 text
Result ܕͷԠ༻
• Promise Ͱ then() ͕ await (yield*) ʹஔ͖͑ΒΕͨ
// before
doX().then((x) =>
doY(x).then((y) =>
doZ(y).then((z) =>
// ...
)
)
);
// after
const x = await doX();
const y = await doY(x);
const z = await doZ(y);
// ...
Slide 48
Slide 48 text
Result ܕͷԠ༻
• Result Ͱಉ͡Α͏ʹஔ͖͑ΒΕΔ → ΤϥʔϋϯυϦϯάΛ؆ུԽ
// before
const x = doX();
if (!x.isOk) return x;
const y = doY(x.value);
if (!y.isOk) return y;
const z = doZ(y.value);
if (!z.isOk) return z;
// ...
// after
const x = yield* doX();
const y = yield* doY(x);
const z = yield* doZ(y);
// ...
Slide 49
Slide 49 text
Result ܕͷԠ༻
• ׂ࣮Ѫ (ઌ΄Ͳͱಉ͡ susisu/tskaigi2025 ʹશ൛͕͋Γ·͢)
• run() ͷఆٛ Promise ͷ࣌ͱ΄΅ಉ͡ (then() ͕ҟͳΔ͚ͩ)
type Comp = Generator, T, any>;
declare function run(comp: Comp): Result;
function* myFunc(): Comp { /* ... */ }
const result = run(myFunc()); // result: Result
Slide 50
Slide 50 text
Result ܕࣗମΛ Iterable ʹ͢Δ
• Promise ͱҟͳΓɺResult ܕࣗମΛ Iterable ʹͰ͖Δ
• yield* ࣜΛ͏ͱ͖ͷมΛखͰॻ͘ඞཁ͕ͳ͘ͳͬͯศར
class Ok {
// ...
*[Symbol.iterator](): Comp {
return yield this;
}
}
Slide 51
Slide 51 text
͜͜·Ͱͷ·ͱΊ
• δΣωϨʔλΛͬͨςΫχοΫ Promise Ҏ֎ʹԠ༻͕Մೳ
• yield ͞ΕͨͱΓͷܭࢉΛ্ख͚ͬͭ͘͘ΒΕͨΒྑ͍ (Ϟφυ)
• ྫͱͯ͠ Result ܕͷ߹Λհ
• ଞʹ͍Ζ͍Ζ
• neverthrow, E
ff
ect ͳͲͷϥΠϒϥϦ
• ύʔαίϯϏωʔλ
• etc.