I'm having difficulty with deferred promises. I am faced with a rather messy string:
me, company name, SSQ ID, the company you are working for (Extraction/XTR/8North) and the tier assigned to your company in question #17.
|Y132~
|Y133~
|Y134~
|Y138~
|Y139~
|Y140~
|Y141~
|Y142~
|Y143~
which requires me to replace each occurrence of "|Y000~" with a URL link. The code for this part is functional. However, I am struggling to implement a promise that waits for the function (involving deferral of multiple promises) to complete before proceeding further.
My "convertString" function has the following snippet:
getAllClusterLinks(indices, returnString)
returnString = $scope.returnString;
Below is the convertString function code:
function convertClusterText(questions, field) {
angular.forEach(questions, function (value, key) {
if (value.vchTextBeforeQuestionCluster != null) {
var str = value.vchTextBeforeQuestionCluster;
// Code to replace various elements in the string
if (returnString.indexOf("|Y") >= 0) {
// Handling links in the string
if (indices.length > 1) {
// Calling getAllClusterLinks function
getAllClusterLinks(indices, returnString)
.then(function () {
returnString = $scope.returnString;
})
value.vchTextBeforeQuestionCluster = returnString;
}
else {
// Getting hyperlink
// Setting up link in the string
}
}
else {
// Handling when no |Y code is found
}
}
});
};
It is crucial for "getAllClusterLinks" to finish executing before moving on to the next line. Here is the code for "getAllClusterLinks":
function getAllClusterLinks(indices, returnString) {
var promises = [];
var times = 0
var endIndex = 0;
angular.forEach(indices, function (value, key) {
// Looping through indices and getting link codes
var promise = getClusterLinks(linkCode, returnString);
promises.push(promise);
})
return $q.all(promises);
}
function getClusterLinks(linkCode, returnString) {
var deferred = $q.defer();
// Code to fetch hyperlink data and set up link
return deferred.promise;
}
The current code functions correctly, but I need it to finalize before setting the line
returnString = $scope.returnString;
.
I attempted this approach, which did not yield the desired result:
getAllClusterLinks(indices, returnString)
.then(function () {
returnString = $scope.returnString;
})
Your help in resolving this issue would be greatly appreciated!