Skip to content

Instantly share code, notes, and snippets.

@timaschew
Created October 25, 2016 14:03
Show Gist options
  • Save timaschew/22e0b11d8c27514972e8769d6848f596 to your computer and use it in GitHub Desktop.
Save timaschew/22e0b11d8c27514972e8769d6848f596 to your computer and use it in GitHub Desktop.
generators + promises = awesome code flow
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)
})
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
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