Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Last active April 28, 2016 21:56
Show Gist options
  • Save daniel-j-h/dd957c04c3b49b1604ccbb100ba85dd9 to your computer and use it in GitHub Desktop.
Save daniel-j-h/dd957c04c3b49b1604ccbb100ba85dd9 to your computer and use it in GitHub Desktop.
ES6 untangling callback 🍝
'use strict';
const fs = require('fs');
function spawn(generator) {
let it = generator(function (err, data) {
if (err) it.throw(err);
return it.next(data);
});
it.next();
}
function main() {
spawn(function* (resume) {
let content = yield fs.readFile(__filename, resume);
yield fs.writeFile(__dirname + '/dup.js', content, resume);
});
}
if (require.main === module) { main(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment