Created
January 1, 2017 17:34
-
-
Save gt3/a7db07ef7a0db0dd6c1bcd0fef2ec6a8 to your computer and use it in GitHub Desktop.
js-csp timers use case for alts
This file contains 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
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