Created
December 21, 2016 23:24
-
-
Save uberbrady/2b575400fcb286c2545b0160e081d7e5 to your computer and use it in GitHub Desktop.
Using 'co' with Thunks instead of Promises
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
const co = require('co') | |
function gen () { | |
console.warn('Beginning wait for gen') | |
return (callback) => { | |
setTimeout( () => { | |
console.warn("waiting finished for 'gen'") | |
callback(null,7) | |
},2000) | |
} | |
} | |
function gen2 () { | |
console.warn('Beginning wait for gen2') | |
return (callback) => { | |
setTimeout( () => { | |
console.warn("waiting finished for 'gen2'") | |
callback(null,11) | |
},2000) | |
} | |
} | |
function * main () { | |
console.warn('main invoked!') | |
let g1 = yield gen() | |
console.warn('okay, that was gen') | |
let g2 = yield gen2() | |
console.warn(`Yay, done! Results are: ${g1} and ${g2}`) | |
console.warn('main is finished') | |
} | |
co(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment