Created
November 14, 2015 14:53
-
-
Save qrg/1e692f5b70b05533c495 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
| #!/usr/bin/env node | |
| 'use strict'; | |
| import 'babel-polyfill'; | |
| import fs from 'fs'; | |
| const PREFIX = './sample/'; | |
| const read = (file) => { | |
| fs.readFile(PREFIX + file, 'utf-8', (err, data) => { | |
| if (err) { | |
| return console.error(err); | |
| } | |
| data = data.replace('\n', ''); | |
| console.log(data); | |
| g.next(data); | |
| }) | |
| }; | |
| const gfn = function*(file) { | |
| try { | |
| let y = yield read(file); | |
| while (y !== 'goal.') { | |
| y = yield read(y); | |
| } | |
| } catch (err) { | |
| console.error(err); | |
| } | |
| }; | |
| const g = gfn('a.txt'); | |
| g.next(); |
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
| #!/usr/bin/env node | |
| 'use strict'; | |
| import 'babel-polyfill'; | |
| import fs from 'fs'; | |
| const PREFIX = './sample/'; | |
| const promise = (file) => { | |
| return new Promise((resolve, reject) => { | |
| fs.readFile(PREFIX + file, 'utf-8', (err, data) => { | |
| if (err) { | |
| return reject(new Error(err)); | |
| } | |
| resolve(data); | |
| }); | |
| }).then((val) => { | |
| val = val.replace('\n', ''); | |
| console.log(val); | |
| if (val === 'end') { | |
| return; | |
| } | |
| return promise(val); | |
| }); | |
| }; | |
| promise('a.txt'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment