Skip to content

Instantly share code, notes, and snippets.

@gt3
Created January 1, 2017 17:34
Show Gist options
  • Save gt3/a7db07ef7a0db0dd6c1bcd0fef2ec6a8 to your computer and use it in GitHub Desktop.
Save gt3/a7db07ef7a0db0dd6c1bcd0fef2ec6a8 to your computer and use it in GitHub Desktop.
js-csp timers use case for alts
let csp = require('js-csp')
let START, SHOULD_END, END
const app = () => {
START = +(new Date);
const ch = csp.chan();
csp.go(function* () {
yield csp.timeout(1000);
yield csp.put(ch, 42);
});
csp.go(function* () {
const cancel = csp.timeout(300);
const result = yield csp.alts([ch, cancel]);
console.log("Has been cancelled?", result.channel === cancel);
SHOULD_END = +(new Date);
});
}
process.on('exit', () => {
END = +(new Date);
console.info("end: %dms \t should-end: %dms", END - START, SHOULD_END - START);
});
app()
/**** OUTPUT: end: 1003ms should-end: 325ms ****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment