So far, the suggestions I've seen to remedy my issue don't seem to apply, or I have a big misunderstanding (I haven't touched full stack in quite a while). I've got a service that successfully hits a Spring Boot backend to get a list of processes:
function getReportProcesses(){
var deferred = $q.defer();
var url = "fermentationReports/processes";
httpService.get(url).then(function (data){
deferred.resolve(data);
}, function(msg){
var errMsg = "failed to get report processes: " + msg;
deferred.reject(errMsg);
});
return deferred.promise;
}
My objective is to hit the service from a dropdown factory, turn the list into JSON, and pass it to the dropdown as populated by the dropdown directive. When the directive calls the following line to get the JSON:
var data = ReportDdlFactory.getReportName();
addAllTo(data, $scope.addAll);
it doesn't wait for the httpService in getReportName() to return before adding data as undefined to the scope. This is the original method in the factory:
function getReportName() {
try {
FermentationReportsService.getReportProcesses().then(function (data) {
if (data.processes !== undefined) {
var output = {"data": []};
for (var i = 0; i < data.processes.length; i++) {
var currentLine = {"value": data.processes[i], "label": data.processes[i]};
output.data.push(currentLine);
}
return output;
} else {
return {
"data": [
{"value": "ERROR OBTAINING PROCESSES", "label": "ERROR OBTAINING PROCESSES"}]
};
}
});
} catch (err) {
console.log("error caught: " + err);
}
}
I tried to change it to use async/await since getReportProcesses() already returns a promise:
async getReportName() {
try {
await FermentationReportsService.getReportProcesses().then(function (data) {
if (data.processes !== undefined) {
var output = {"data": []};
for (var i = 0; i < data.processes.length; i++) {
var currentLine = {"value": data.processes[i], "label": data.processes[i]};
output.data.push(currentLine);
}
return output;
} else {
return {
"data": [
{"value": "ERROR OBTAINING PROCESSES", "label": "ERROR OBTAINING PROCESSES"}]
};
}
});
} catch (err) {
console.log("error caught: " + err);
}
}
But I always get this from jshint:
14 | async getReportName() {
^ Expected an assignment or function call and instead saw an expression.
14 | async getReportName() {
^ Missing semicolon.
14 | async getReportName() {
^ Missing semicolon.
17 | await FermentationReportsService.getReportProcesses().then(function (data) {
^ Expected an assignment or function call and instead saw an expression.
17 | await FermentationReportsService.getReportProcesses().then(function (data) {
^ Missing semicolon.
8 | getReportName: getReportName,
^ 'getReportName' is not defined.
14 | async getReportName() {
^ 'getReportName' is not defined.
14 | async getReportName() {
^ 'async' is not defined.
I feel like I may be making this harder than it needs to be, or maybe I'm missing something simple?
EDIT:
After more research I determined that the app is running as 'strict', forcing ECMA 5. Is there an approach to force the response before continuing without using an ES6 approach?
functionkeyword when making itasync. That being said, your problem in both cases is just that you don't return any value. You need toreturn FermentationReportsService.getR.... The Promise will then resolve to that valuereturn FermentationReportsService.getR..., I can't do that. It comes as a list, so I need to iterate over them and return the JSON object I created which happens after the for-loop. Does returning theoutputvariable make the process invalid?