18

我将如何返回一个承诺但立即调用它的失败块?这是一种粗糙的方法:

if (fail) {
    var q = $q.deferred();

    $timeout(function() {
        q.reject("")
    }, 1);

    return q.promise;
} else {
  return $http.get("/").then(function(data) {});
}
4

2 回答 2

18
if( fail ) {
    return $q.reject(yourReasonObject);
}
else ...

参考这里:)

于 2013-09-24T07:01:56.113 回答
1
function setLike(productId){
    return new Promise(function(succeed, fail) {
        if(!productId) throw new Error();
        jQuery.ajax({
            success: function (res) {
                succeed(res)
            }
        })
    })
}


    setLike(id).then(function(){

       //render

    }).catch(function(e){})
于 2017-08-21T10:20:39.537 回答