Skip to content

Instantly share code, notes, and snippets.

@TerrorJack

TerrorJack/channel.mjs

Last active Aug 7, 2019
Embed
What would you like to do?
Async JavaScript Channels
function newThunk(f) {
let t = () => {
const r = f();
t = () => r;
return r;
};
return () => t();
}
function newNode() {
let r,
p = new Promise(resolve => {
r = resolve;
});
return { promise: p, resolve: r, next: newThunk(newNode) };
}
export class Channel {
constructor() {
this.takeNode = newNode();
this.putNode = this.takeNode;
Object.seal(this);
}
take() {
const r = this.takeNode.promise;
this.takeNode = this.takeNode.next();
return r;
}
put(r) {
this.putNode.resolve(r);
this.putNode = this.putNode.next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.