2016-09-16 103 views
0

我正在努力推遲承諾。我有一個非常難看的字符串:函數中的延遲承諾不推遲函數的執行

我,公司名稱,SSQ ID,您正在爲之工作的公司(Extraction/XTR/8North)以及問題#17中分配給貴公司的層級。

| Y132〜
| Y133〜

| Y134〜

| Y138〜
| Y139〜
| Y140〜

| Y141〜
| Y142〜
| Y143 〜

我必須替換每個出現的「| Y000〜」w ith URL鏈接。代碼的那部分工作正常。問題是,我無法弄清楚如何使用承諾等待函數(包括推遲多個承諾)等待函數完成後再繼續。

我有這個在我的 「convertString」 功能:

getAllClusterLinks(indices, returnString) 
returnString = $scope.returnString; 

這裏是convetString功能:

function convertClusterText(questions, field) { 
    angular.forEach(questions, function (value, key) { 
     if (value.vchTextBeforeQuestionCluster != null) { 
      var str = value.vchTextBeforeQuestionCluster; 
      var returnString = str.replaceAll('|B', '<b>'); 
      returnString = returnString.replaceAll("|b", "</b>"); 
      returnString = returnString.replaceAll("|+", "<br/>"); 
      returnString = returnString.replaceAll("|L", "<"); 
      returnString = returnString.replaceAll("|R", ">"); 
      returnString = returnString.replaceAll("|T", "<table border='1'>"); 
      returnString = returnString.replaceAll("|/T", "</table>"); 
      returnString = returnString.replaceAll("|S", "<tr>"); 
      returnString = returnString.replaceAll("|/S", "</tr>"); 
      returnString = returnString.replaceAll("|C", "<td>"); 
      returnString = returnString.replaceAll("|/C", "</td>"); 
      returnString = returnString.replaceAll("|A", "&#39;"); 
      returnString = returnString.replaceAll("|Q", "&amp;"); 
      returnString = returnString.replaceAll("|P", "&#59;"); 
      returnString = returnString.replaceAll("|W", "&#34;"); 
      returnString = returnString.replaceAll("|H", "<hr style='width: 100%;'>"); 
      returnString = returnString.replaceAll("|U", "<span style='text-decoration:underline'>"); 
      returnString = returnString.replaceAll("|x", "</span>"); 
      returnString = returnString.replaceAll("|N", "<span style='color:black'>"); 
      returnString = returnString.replaceAll("|D", "<span style='color:blue'>"); 
      returnString = returnString.replaceAll("|E", "<span style='color:red'>"); 
      returnString = returnString.replaceAll("|G", "<span style='color:gray'>"); 
      if (returnString.indexOf("|Y") >= 0) { 
       var indices = []; 
       var linkCode; 

       indices = getIndicesOf("|Y", returnString, true); 

       if (indices.length > 1) { 

        getAllClusterLinks(indices, returnString) 
        .then(function() { 
         returnString = $scope.returnString; 

        }) 
          value.vchTextBeforeQuestionCluster = returnString; 



       } 
       else { 
        linkCode = getLink(returnString); 
        contractorService.gethyperlink(linkCode) 
        .success(function (data) { 
         var vchUrl = data[0].vchUrl; 
         var docID = getDocumentID(vchUrl); 
         var vchLinkName = data[0].vchLinkName; 
         questions[key].document = docID; 
         var yay = '<a href="" ng-click="getDocument(cluster)">' + vchLinkName + '</a>'; 
         var yCode = "|Y" + linkCode + "~"; 
         returnString = returnString.replaceAll(yCode, yay); 
         value.vchTextBeforeQuestionCluster = returnString; 
        }) 
       } 

      } 
      else { 
       value.vchTextBeforeQuestionCluster = returnString; 

      } 


     } 
    }); 
}; 

我需要 「getAllClusterLinks」 執行的下一行之前完成。這裏是「getAllClusterLinks」代碼:

function getAllClusterLinks(indices, returnString) { 
    var promises = []; 
    var times = 0 
    var endIndex = 0; 
    angular.forEach(indices, function (value, key) { 
     endIndex = getEndIndicesOf("~", returnString, value); 
     linkCode = getMultiLinks(returnString, value, endIndex) 
     var promise = getClusterLinks(linkCode, returnString); 
     promises.push(promise); 
    }) 

    return $q.all(promises); 
} 
function getClusterLinks(linkCode, returnString) { 
    var deferred = $q.defer(); 
    $scope.returnString = returnString; 
    contractorService.gethyperlink(linkCode) 
    .success(function (data) { 
     var vchUrl = data[0].vchUrl; 
     var end = vchUrl.length; 
     var docID = vchUrl.substring(vchUrl.indexOf("=") + 1, end); 
     var vchLinkName = data[0].vchLinkName; 
     var yay = '<a href="" ng-click="getDocument(' + docID + ')">' + vchLinkName + '</a>'; 
     var yCode = "|Y" + linkCode + "~"; 
     $scope.returnString = $scope.returnString.replaceAll(yCode, yay); 
    }) 
    return deferred.promise; 

} 

上面的代碼按預期工作,但我需要它來設置線路returnString = $scope.returnString;之前首先完成。

試過,但它不工作:

     getAllClusterLinks(indices, returnString) 
        .then(function() { 
         returnString = $scope.returnString; 

        }) 

任何幫助是非常感謝!

+0

您也在函數內設置'$ scope.returnString = returnString;'。更詳細地解釋這應該如何工作。我想你想要每次都返回字符串,但不清楚究竟是什麼目標 – charlietfl

回答

1

$ q.all(承諾)返回一個承諾。你應該可以使用then()。

getAllClusterLinks(indices, returnString).then(function() { 
    returnString = $scope.returnString; 
}); 

[https://docs.angularjs.org/api/ng/service/ $ Q] [1]

編輯:你應該解決您的延期對象

旁註:我相信成功()已經過時,你應該也使用。那麼

function getClusterLinks(linkCode, returnString) { 
    var deferred = $q.defer(); 
    $scope.returnString = returnString; 
    contractorService.gethyperlink(linkCode) 
    .success(function (data) { 
     var vchUrl = data[0].vchUrl; 
     var end = vchUrl.length; 
     var docID = vchUrl.substring(vchUrl.indexOf("=") + 1, end); 
     var vchLinkName = data[0].vchLinkName; 
     var yay = '<a href="" ng-click="getDocument(' + docID + ')">' + vchLinkName + '</a>'; 
     var yCode = "|Y" + linkCode + "~"; 
     $scope.returnString = $scope.returnString.replaceAll(yCode, yay); 
     deferred.resolve(); // resolve here 
    }) 
    return deferred.promise; 

} 
+0

不幸的是,我試過了,「那麼」永遠不會被擊中。 –

+0

你沒有解決你的延期對象。查看我的更新回答 – doge1ord

+0

謝謝!添加deferred.resolve();做到了!太感謝了! –

0

嘗試把下一行的則()回調:

getAllClusterLinks(indices, returnString) 
    .then(function() { 
     returnString = $scope.returnString; 
    }); 

有關內角承諾的詳細信息,請參閱the documentation