Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go で言うところのアレは TypeScript で言うとコレ / Kyoto.なんか #7

Avatar for Susisu Susisu
August 23, 2025

Go で言うところのアレは TypeScript で言うとコレ / Kyoto.なんか #7

Avatar for Susisu

Susisu

August 23, 2025
Tweet

More Decks by Susisu

Other Decks in Technology

Transcript

  1. app server req handler worker API server DB client X

    X ӬଓԽ Ϩεϙϯε main workerα workerβ workerγ ⋮ ܬͷޫ SIGTERM Go͔ʁ
  2. goroutine → async function • ϚϧνεϨου͕ཉ͍͠ͳΒ Web Workers ͳͲ͕ඞཁ͚ͩͲ (async

    function() { await setTimeout(1000); console.log("Pong"); })(); console.log("Ping"); // Ping // (1s) // Pong
  3. context → AbortController • DOM Standard Ͱఆٛ͞Ε͍ͯΔ AbortController ͕࢖͑Δ const

    ac = new AbortController(); startWorker(ac.signal); // ... ac.abort();
  4. context → ? • ୯ʹΦϒδΣΫτΛҾ਺Ͱ౉͢ͷ͕جຊ͚ͩͲ... const ctx = { requestId:

    "c0ffee" }; handler(ctx); // handler: ctx.requestId; // = "c0ffee"
  5. context → AsyncLocalStorage • Ҿ਺Λ౉͢ͷ͕೉͍࣌͠͸ AsyncLocalStorage (ඇඪ४) ͕ศར͔΋ const storage

    = new AsyncLocalStorage<{ requestId: string }>(); const ctx = { requestId: "c0ffee" }; storage.run(ctx, () => { handler(); }); // handler: storage.getStore()?.requestId; // = "c0ffee"
  6. channel • goroutine ؒͷσʔλͷૹड৴ c := make(chan int) go func()

    { for n := 0; n < 10; n++ { c <- n } close(c) }() for n := range c { fmt.Println(n) } // 0, 1, 2, ..., 9
  7. channel → Streams • Streams API ͰࣅͨΑ͏ͳ͜ͱ͕Ͱ͖·͢ const { readable,

    writable } = new TransformStream<number, number>(); (async function() { const writer = writable.getWriter(); for (let n = 0; n < 10; n++) { await writer.write(n); } await writer.close(); })(); for await (const n of readable) { console.log(n); } // 0, 1, 2, ..., 9
  8. channel → await • await ͰΑ͍Ͱ͢Ͷ const promise = (async

    function() { // ... })(); // ... await promise;
  9. ·ͱΊʢ̍ʣ • Go Ͱݴ͏ͱ͜ΖͷΞϨ͸ TypeScript Ͱݴ͏ͱίϨ • goroutine → async

    function • context → AbortController, AsyncLocalStorage • channel → Streams ͳͲ