Last active
November 10, 2015 14:05
-
-
Save qrg/4094a8699d32aa472d03 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
| 'use strict'; | |
| // http://jxck.hatenablog.com/entry/2014-01-12/generator-screencaset | |
| const co = (gen) => { | |
| const g = gen(); | |
| const done = (err, data) => { | |
| if (err) { | |
| return g.throw(err); | |
| } | |
| const n = g.next(data); | |
| if (n.done) { | |
| return; | |
| } | |
| n.value(done); | |
| }; | |
| return () => g.next().value(done); | |
| } | |
| const thunkify = (fn) => { | |
| return (...args) => { | |
| return (callback) => { | |
| args.push(callback); | |
| fn(...args); | |
| } | |
| }; | |
| } | |
| // ============================================ | |
| import fs from 'fs'; | |
| import 'babel-polyfill'; | |
| co(function* generator() { | |
| const read = thunkify(fs.readFile); | |
| try { | |
| const prefix = './sample/'; | |
| const filename = 'a.txt'; | |
| const path = `${prefix}${filename}`; | |
| let b = yield read(path, 'utf-8'); | |
| b = `${prefix}${b.replace('\n', '')}`; | |
| let c = yield read(b, 'utf-8'); | |
| c = `${prefix}${c.replace('\n', '')}`; | |
| let d = yield read(c, 'utf-8'); | |
| d = `${prefix}${d.replace('\n', '')}`; | |
| let result = yield read(d, 'utf-8'); | |
| result = result.replace('\n', ''); | |
| console.log(result); | |
| } catch (err) { | |
| console.error(err); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment