Skip to content

Instantly share code, notes, and snippets.

@kossnocorp
Last active August 29, 2015 14:14
Show Gist options
  • Save kossnocorp/867c097cc34801ed272d to your computer and use it in GitHub Desktop.
Save kossnocorp/867c097cc34801ed272d to your computer and use it in GitHub Desktop.
var csp = require('js-csp');
var isTrue = function(id) {
return csp.go(function*() {
yield csp.timeout(Math.floor(Math.random() * 5000));
console.log(`~ done: ${id}`);
return { id, yup: Math.random() > .5 };
});
}
csp.go(function*() {
var resultCh = [1, 2, 3].reduce(function(ch, id) {
return csp.go(function*() {
let res = yield csp.take(isTrue(id));
if (ch) {
return (yield csp.take(ch)).concat(res);
} else {
return [res];
}
});
}, null);
var result = yield csp.take(resultCh);
console.log(`> result: ${JSON.stringify(result)}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment