ECMAScript 6 生成器代码异步 rocore
#广州# OSC源创会第31期(12月27日)开始报名,OSC自曝家丑!
rocore 是 ECMAScript 6 生成器代码异步工具。rocore 不修改noejs编码原型,使用ctrl next()提供足量的代码序列控制。
示例
var R = require('rocore'), app = R.Application(), server = require('http').createServer(), url = require('url'), fs = require('fs');server.on('request', function (req, res) { app.run(req, res, function (exist) { if (!exist) { res.writeHead(404); res.end('Could not find ' + url.parse(req.url).pathname); } else { console.log('response done.'); } });});server.listen(8000);app.get('/:username', savef, somef);// app.get('/:username/:id', dosome, then);function* savef(req, res, ynext, next) { var start = Date.now(), it = yield fs.readFile(__dirname + 'test.conf', {encoding:'utf8'}, ynext), err = it[0], data = it[1]; if (err) throw err; yield fs.writeFile(__dirname + 'save.conf', {encoding:'utf8'}, ynext); yield next(ynext); var end = Date.now(); res.writeHead(200); res.end('use time: ' + (end - start));}function* somef(req, res, ynext, next) { // return after 1s yield setTimeout(function () { ynext(arguments); }, 1000);}