写了 node.js
之后 写的最多的就是
if(err) return next(err);
这种情况正常吗?
更新:我用的是 express.js
做 web
开发。
给你个参考吧,前端时间看 HASKELL,用js实现了一个 Maybe。Haskell里面一种处理错误的方式。仅供参考。
// Date Maybe x = Nothing | Just x
function Maybe(value){
this._valid = true;
this.value = value;
}
Maybe.prototype = {
constructor:Maybe,
// _bind:: Maybe a -> (a -> Maybe b) -> Maybe b
// bind = _bind this
bind:function (fn) {
if(!this._valid) { return Nothing; }
return fn.call(null,this.value);
},
// _then:: Maybe a -> Maybe b -> Maybe b
// then = _then this
then:function(fn) {
return fn.call(null);
},
toString:function(){
if (!this._valid) { return "Nothing"; }
return "Just " + this.value.toString();
}
};
var Nothing = new Maybe(undefined);
Nothing._valid = false;
function Just(value)
{
return new Maybe(value);
}
Maybe.Nothing = Nothing;
Maybe.Just = Just;
实例:
// subOne:: int -> Maybe int
function subOne(value){
//假设负数是不允许的。
if(value === 0) return Nothing;
value--;
return Just(value);
}
var ret=Just(5).bind(subOne).bind(subOne).bind(subOne).bind(subOne).bind(subOne).bind(subOne);