node-sync 这么好用的库为啥没什么人用?

2016-05-20 15:17:01 +08:00
 haozes

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

})

但就是不知道这玩意为啥没什么人用,难道太坑?

4913 次点击
所在节点    Node.js
28 条回复
sox
2016-05-20 21:45:45 +08:00
@maomaomao001 有哪个包管理器有增量更新的功能,求告知 😂
Jaylee
2016-05-21 00:45:06 +08:00
@sox 增量肯定是没有,不过 npm 3 开始就将依赖扁平化了,大大节省了项目中下载依赖的时间
magicdawn
2016-05-21 09:03:46 +08:00
napsterwu
2016-05-21 09:15:41 +08:00
@haozes 你可以看看 mongoose 是怎么用的 可 callback 可 promise 解决不了回调地狱是你方式不对
julor
2016-05-21 10:53:02 +08:00
为何还停留在如此落后的回调方式,不直接上 golang ?
haozes
2016-05-21 15:04:27 +08:00
@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
return new this.s.promiseLibrary(function(resolve, reject) {
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 接口.
haozes
2016-05-21 15:05:13 +08:00
@julor
这个...Golang 太灰主流了.没法招人啊
julor
2016-05-21 18:17:12 +08:00
@haozes 不认为,难道 nodejs 找来的都很牛逼? golang 招过来 3 天不能贡献代码,此人可开除!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/280037

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX