2014-09-23 110 views
0

我想知道如何在涉及條件時正確處理承諾。Angularjs的處理承諾

例如,我有這樣的代碼:

if(something == true) { 
    loginObj.$login("anonymous").then(function(){ //handling the promise 
    //do something with the promise 
    //do something line 1 
    //do something line 2 
    //do something line 3 
    //do something line 4 
    //do something line 5 
    }); 
} 
else { 
    //do the same thing line 1 
    //do the same thing line 2 
    //do the same thing line 3 
    //do the same thing line 4 
    //do the same thing line 5 
} 

我希望你能看到我的問題。如果something爲真,那麼在執行我的代碼行之前,我將不得不等待解決的承諾。然而,我的else塊包含幾乎所有的代碼行,但我不得不重複自己。

我爲什麼要避免這種重複?

+0

執行重複行的「最後」 – mccainz 2014-09-23 16:29:57

+0

你可以給我一個例子嗎? – tommyd456 2014-09-23 16:30:28

回答

1

放下一個函數來調用?

$scope.doTheSame= function() 
{ 

} 

if(something == true) { 
    loginObj.$login("anonymous").then(function(){ //handling the promise 
    //do something with the promise 
    $scope.doTheSame() 
    }); 
} 
else { 
    $scope.doTheSame() 
} 

又通所需要的參數,或者如果你有工作的範圍對象,你仍然可以訪問它們

+0

對不起,我認爲你錯過了我的觀點。我知道功能 – tommyd456 2014-09-23 16:30:08

+0

所以也許清理問題,你不想寫代碼兩次,有什麼反對使用這個功能? – graphefruit 2014-09-23 16:31:32

1

執行重複行的「最後/總是」塊。

舉例來說,如果使用$ Q,

var outputPromise = getInputPromise() 
.fin(function() { 
    // close files, database connections, stop servers, conclude tests 
}); 

或者,如果使用jQuery ...

$.get("test.php").always(function() { 
    alert("$.get completed with success or error callback arguments"); 
}); 

或者如果你的代碼不能被結構化使得登錄替代方法可以在流動的承諾鏈,那麼你可以簡單地將兩個登錄函數都移除到一個返回承諾然後鏈接該函數的函數中,如下所示。

http://plnkr.co/edit/nJRaBb04JpLdHicgxg2u?p=preview

<!DOCTYPE html> 
<html> 

    <head> 
    <script data-require="[email protected]*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <link rel="stylesheet" href="style.css" /> 
    <script src="script.js"></script> 
    </head> 

    <body> 
    <h1>Hello Plunker!</h1> 
    <button id="loginBtn">Login</button> 

    <script> 

    function login1(){ 
     var $def =$.Deferred(); 
     window.setTimeout(function(){ 
     $def.resolve("loginMethod1"); 
     },1000); 
     return $def.promise(); 
    } 

    function login2(){ 
     var $def =$.Deferred(); 
     window.setTimeout(function(){ 
     $def.resolve("loginMethod2"); 
     },1000); 
     return $def.promise(); 
    } 

    function login(val){ 
     var $def =$.Deferred(); 
     if(val){ 
     login1().then(function(res){ 
      $def.resolve(res); 
     }); 
     } 
     else{ 
     login2().then(function(res){ 
      $def.resolve(res); 
     }); 
     } 
     return $def.promise(); 
    } 

    $("#loginBtn").bind("click",function(){ 
     login(Math.random()>0.5) 
     .then(function(res){ 
      console.log(res + " ...do everythign else here"); 
     }); 
    }); 

    </script> 
    </body> 

</html> 
1
if(something == true) { 
    loginObj.$login("anonymous").then(function(){ //handling the promise 
    //do something with the promise 
    doSomething(); 
    }). 
    catch(function() { // catch code }). 
    finally(function() { // finally code }); 
} 
else { 
    doSomething() 
} 

function doSomething() { 
    //do the same thing line 1 
    //do the same thing line 2 
    //do the same thing line 3 
    //do the same thing line 4 
    //do the same thing line 5 
} 
0

我喜歡用$ q.when()/ $ q.reject()創建在這樣的情況下,一個解決的承諾。

var promise; 
if (something === true) { 
    promise = loginObj.$login("anonymous") 
    .then(
     function(){ //handling the promise 
      //do something with the promise 
     } 
    ); 
} 
else { 
    promise = $q.when("somethingelse"); 
    //promise = $q.reject("somereason"); 
} 
promise.then(
    function (somethingelse) { 
     //do something line 1 
     //do something line 2 
     //do something line 3 
     //do something line 4 
     //do something line 5 
    } 
);