-
-
Save timaschew/22e0b11d8c27514972e8769d6848f596 to your computer and use it in GitHub Desktop.
generators + promises = awesome code flow
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
const Promise = reuqire('bluebird') | |
// mach aus callback style -> promise style | |
// aber dafür muss man "Async" an die Funktionen drangehängt werden | |
const fs = Promise.promisifyAll(require('fs')) | |
module.exports = co(function*(filename, text) { | |
return yield fs.readFileSync(filename, text) | |
}) |
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
const Promise = reuqire('bluebird') | |
const co = Promise.coroutine | |
const write = require('./write') | |
const read = Promisie.promisify(require('./read')) | |
co(function*() { | |
yield write('blub.txt', 'foobar') | |
const content = yield readSync('blub.txt') | |
console.log(content) | |
})() | |
// ODER OHNE IFEE | |
const main = co(function*() { | |
yield write('blub.txt', 'foobar') | |
const content = yield readSync('blub.txt') | |
console.log(content) | |
}) | |
main() // hier wird kein yield oder .then benötigt, da es der entry point ist |
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
const fs = require('fs') | |
module.exports = function(filename, cb) { | |
fs.readFile(filename, cb) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment