Created
February 4, 2025 09:35
-
-
Save acomagu/a87649500cd4e1e12d752afe9cfe90d9 to your computer and use it in GitHub Desktop.
Simple channel with async generator for TypeScript(without buffers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chan<T>() { | |
let p = Promise.withResolvers<T>(); | |
async function* gen() { | |
while (true) { | |
let v; | |
try { | |
v = await p.promise; | |
} catch { | |
return; | |
} | |
p = Promise.withResolvers<T>(); | |
yield v; | |
} | |
} | |
return { | |
...gen(), | |
put: p.resolve, | |
close: p.reject, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment