
https://github.com/ybogdanov/node-sync
node 的回调太蛋疼了,业务一复杂,再加上错误处理,那代码仅只日狗...,看了不少库,co,promise,yield 感觉都不是很方便.这个库,一下把代码精练了许多,用 fiber 处理应该性能问题不大
// Run in a fiber Sync(function(){
// Function.prototype.sync() interface is same as Function.prototype.call() - first argument is 'this' context var result = asyncFunction.sync(null, 2, 3); console.log(result); // 5 // Read file synchronously without blocking whole process? no problem var source = require('fs').readFile.sync(null, __filename); console.log(String(source)); // prints the source of this example itself })
但就是不知道这玩意为啥没什么人用,难道太坑?
1 Septembers 2016-05-20 15:41:41 +08:00 尽可能不要修改内置类型的 prototype see https://v2ex.com/t/190956#r_2061759 |
2 arden 2016-05-20 16:08:53 +08:00 好东东,有这东东,就不用把函数转成 async function 或者 function()*了。 |
3 colatin 2016-05-20 16:15:27 +08:00 每个 fiber 会有个栈,如果在复杂应用里广泛使用,估计会死得很难看 |
4 xjp 2016-05-20 16:23:53 +08:00 如果想用 fib 的话 来这里 http://fibjs.org 孢子社区全站都是用这个写的 |
5 daysv 2016-05-20 16:50:10 +08:00 尽可能不要修改内置类型的 prototype +1 |
6 Jaylee 2016-05-20 17:00:05 +08:00 还是更喜欢 await / async |
7 tomoya92 2016-05-20 17:54:10 +08:00 用 async 顺手了 |
8 sox 2016-05-20 17:58:50 +08:00 via Android 搞反了吧, co 还不方便 |
9 fds 2016-05-20 18:12:10 +08:00 因为不喜欢 fiber ,运行时程序流程变复杂了 |
10 djyde 2016-05-20 18:14:16 +08:00 第三方入侵你的 prototype 是很危险的。 何况有 async/await |
11 mercurylanded 2016-05-20 18:35:47 +08:00 用 rxjs |
12 alexapollo 2016-05-20 19:25:28 +08:00 代码不兼容?栈的大小过大? |
14 haozes OP @Septembers 这个应该不算修改 protype 吧 |
15 orcusfox 2016-05-20 20:02:39 +08:00 es6 已经自带 promise 了 不再需要其他的库 |
16 maomaomao001 2016-05-20 20:05:15 +08:00 via Android 已得 node 恐惧症..... 求 node 可视化包管理工具 node 一言不合就开始联网下东西.....还下的特别慢慢 和 gradle 有的一比... |
17 sox 2016-05-20 20:15:46 +08:00 via Android @maomaomao001 下东西难道不是因为你执行了需要连接网络的操作,怪 node 干什么 |
18 haozes OP @napsterwu promise 是无法完全解决回调地狱问题的,而且会传染所有的地方都得用 promise |
19 haozes OP @maomaomao001 比 gradle 还是好多了,那货经常下不来 |
20 maomaomao001 2016-05-20 21:36:29 +08:00 @sox 不是啊,明明下载过的东西,比如 ts 项目,它为什么每次都要下一遍 typescript 全部东西....速度还那么慢....就不能用本地的吗? 问题,到底又没用比较好用可视化管理工具? |
21 sox 2016-05-20 21:45:45 +08:00 @maomaomao001 有哪个包管理器有增量更新的功能,求告知 |
23 magicdawn 2016-05-21 09:03:46 +08:00 https://github.com/magicdawn/promise.ify 可破 co 升级到 async/await 指南 - [] Promise.all - {} https://github.com/magicdawn/promise.obj - 并发 https://github.com/magicdawn/promise.map - 超时 https://github.com/magicdawn/promise.timeout - 重试 https://github.com/magicdawn/promise.retry - sleep https://github.com/magicdawn/promise.delay 哎~我只是手痒造轮子玩 |
24 orcusfox 2016-05-21 09:15:41 +08:00 @haozes 你可以看看 mongoose 是怎么用的 可 callback 可 promise 解决不了回调地狱是你方式不对 |
25 julor 2016-05-21 10:53:02 +08:00 via Android 为何还停留在如此落后的回调方式,不直接上 golang ? |
26 haozes OP @napsterwu Collection.prototype.update = function(selector, document, options, callback) { var self = this; // Add ignoreUndfined if(this.s.options.ignoreUndefined) { optiOns= shallowClone(options); options.ignoreUndefined = this.s.options.ignoreUndefined; } // Execute using callback if(typeof callback == 'function') return updateDocuments(self, selector, document, options, callback); // Return a Promise updateDocuments(self, selector, document, options, function(err, r) { if(err) return reject(err); resolve(r); }); }); } mongoose 用这种方法解决的,判断 callback 类型,想想这代码写得有多费劲... https://github.com/digitaldesignlabs/es6-promisify 这个库倒可以稍微简单一点把 callback 接口转换成 promise 接口. |