express - 写了 node.js 之后 写的最多的就是 if(err) return next(err); 这样正常吗?
黄舟
黄舟 2017-04-17 13:34:20
[Node.js讨论组]

写了 node.js 之后 写的最多的就是

if(err) return next(err);

这种情况正常吗?


更新:我用的是 express.jsweb 开发。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
巴扎黑

给你个参考吧,前端时间看 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);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号