【问题标题】:Deferred Promises in Function does not Defer Execution of Function函数中的延迟承诺不会延迟函数的执行
【发布时间】:2017-06-09 13:23:35
【问题描述】:

我正在为延期承诺而苦苦挣扎。我有一个非常难看的字符串:

我、公司名称、SSQ ID、您工作的公司(Extraction/XTR/8North)以及分配给您的公司的层级 #17。

|Y132~
|Y133~

|Y134~

|Y138~
|Y139~
|Y140~

|Y141~
|Y142~
|Y143~

我必须用 URL 链接替换每次出现的“|Y000~”。那部分代码工作正常。问题是我不知道如何使用承诺等待函数(包括延迟多个承诺)等到函数完成后再继续。

我的“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;

                    })

非常感谢任何帮助!

【问题讨论】:

  • 你也在函数内部设置$scope.returnString = returnString;。更详细地解释这应该如何工作。我认为您希望每次都返回字符串,但不清楚确切的目标是什么

标签: javascript angularjs promise angular-promise


【解决方案1】:

$q.all(promises) 返回一个承诺。你应该可以使用 then() 。

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

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

编辑:你应该解决你的延迟对象

旁注:我相信success()已经被弃用了,你也应该使用.then

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;

}

【讨论】:

  • 不幸的是,我试过了,“then”永远不会被击中。
  • 您没有解决您的延迟对象。查看我的更新答案
  • 谢谢!添加 deferred.resolve();做到了!谢谢一百万!
【解决方案2】:

尝试将后续行放在 then() 回调中:

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

有关 Angular 中的承诺的更多信息,请参阅the documentation

【讨论】:

    猜你喜欢
    • 2015-03-08
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多