Skip to content

Instantly share code, notes, and snippets.

@acomagu
Created February 4, 2025 09:35
Show Gist options
  • Save acomagu/a87649500cd4e1e12d752afe9cfe90d9 to your computer and use it in GitHub Desktop.
Save acomagu/a87649500cd4e1e12d752afe9cfe90d9 to your computer and use it in GitHub Desktop.
Simple channel with async generator for TypeScript(without buffers)
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