-
-
Save kossnocorp/867c097cc34801ed272d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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