【问题标题】:JavaScript promise confusionJavaScript 承诺混淆
【发布时间】:2014-03-25 09:48:45
【问题描述】:

我正在尝试掌握 javascript 承诺的概念。但我遇到了一些问题。我在本地设置了一个很小的 ​​web 服务(不要生气,web 服务不符合约定)。这里有一些关于它的细节

/login/<username>/<password> ==> 登录系统,正确的用户名和密码都是noor

如果用户登录,可以拨打/car/<brand>/<color>/<plate_number>

我没有对颜色、品牌、车牌号的类型进行任何验证

这个很好用,我正在记录并添加一辆车

$.ajax({type: "GET",url: url+"/login/noor/noor"})
                .then(function( data, textStatus, jqXHR ) {console.log("login success");},function(){console.log("login error");})
                .then($.ajax({type: "GET",url: url+"/car/1/1/1"}))
                .then(function(){console.log("car added");},function(){console.log("car not added");});

这个完美显示错误,因为使用了无效的url:

$.ajax({type: "GET",url: url+"/carasdsad/1/1/1"})
                .then(function(){console.log("car added");},function(){console.log("car not added");});

"/carasdsad/1/1/1" 是一个无效的 url 并且 car not added 被返回

我遇到了这个问题。下面的代码使用上面的代码。我期待 car not added 会显示,但它的显示 car added

$.ajax({type: "GET",url: url+"/login/noor/noor"})
                .then(function( data, textStatus, jqXHR ) {console.log("login success");},function(){console.log("login error");})
                .then($.ajax({type: "GET",url: url+"/carasdsad/1/1/1"}))
                .then(function(){console.log("car added");},function(){console.log("car not added");});

虽然 "/carasdsad/1/1/1" 在第二次调用中是无效的 url,但上述代码返回 car added

【问题讨论】:

    标签: javascript jquery promise


    【解决方案1】:

    根据specthen 方法中的非函数参数被忽略。

    您的代码简化为最小大小写如下所示:

    Promise.resolve(true)
        .then(Promise.reject("some err"))
        .catch(console.log.bind(console, "fail"));
    

    并且需要以这种方式重写它以捕获错误:

    Promise.resolve(true)
        .then(function(){ return Promise.reject("some err") })
        .catch(console.log.bind(console, "fail"));
    

    【讨论】:

    • 不知何故,我仍然认为像 BlueBird 这样的真正承诺库不会发生这种情况 :-)
    • real 库下是什么意思?是的,jQuery 没有遵循 A+ 规范,但我在 Chrome 中使用原生 Promises 测试了我的示例。
    • 不过,deferred.then 期望函数作为参数。
    • @JanDvorak:嗯,Bluebird 的行为是一样的,但它会记录一个关于这个新手错误的警告,即传递承诺而不是回调:-)
    【解决方案2】:

    一个简单的 Promise 实现

    (请使用 Chrome/Firefox)

    function mPromise(executor) {
    this.status = null;
    this.result = null;
    that=this;
    executor(function(result) {
        that.status = "fullfiled";
        that.result = result;
    }, function(result) {
        that.status = "rejected";
        that.result = result;
    })};
    
    mPromise.prototype.then = function(f, r) {
    if (this.status == "fullfiled") {
        f(this.result);
    } else {
        r(this.result);
    }}
    

    首先,创建一个 Promise 对象:

    var a = new mPromise(function(fFlagFun, rFlagFun) {
    setTimeout(function() { //using setTimeout mimic remote request
        fFlagFun("yes"); //here you can try rFlagFun("no") by hand;
    }, 3000);});
    

    然后3s后,执行这些代码sn-p:

    a.then(function(e) {console.log(e);}, function(e) {console.log(e);})
    

    如果你用其他形式测试它可能会失败。我希望上面的代码能给你一个Promise概念的概述。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2017-12-08
    相关资源
    最近更新 更多