Sorry if this question has been solved before, I have checked this, this one, too (which, I thought, was a good lead, but I failed to understand the accepted answer) and lastly this one.
I have this case in a switch statement:
function setFieldTransformation(iteration) {
//Omitting first hundred lines and switch declaration...
case "customer.subscriptionSet":
var subscriptionCode;
if (iteration.oldValue && iteration.newValue) {
if (Array.isArray(iteration.oldValue) && Array.isArray(iteration.newValue)) {
subscriptionCode = iteration.oldValue[0].service;
} else {
subscriptionCode = iteration.oldValue.service;
}
} else if (iteration.oldValue) {
if (Array.isArray(iteration.oldValue)) {
subscriptionCode = iteration.oldValue[0].service;
} else {
subscriptionCode = iteration.oldValue.service;
}
} else {
if (Array.isArray(iteration.newValue)) {
subscriptionCode = iteration.newValue[0].service;
} else {
subscriptionCode = iteration.newValue.service;
}
}
return getSubscriptionName(subscriptionCode);
break;
}
What I'm trying to achieve:
Here I check for a subscription code, then I call this function:
function getSubscriptionName(subscriptionCode) {
ServiceService.getService(subscriptionCode)
.then(function(response){
var subscriptionName = response.data.name
return response.data.name;
})
}
Which calls a service to get the subscription name. That string is then passed through this function and printed in a table:
function transformDetailValuesReworked(iteration) {
//...
var fieldSetTransformation = setFieldTransformation(iteration);
return fieldSetTransformation;
}
What actually happens:
If I understand correctly the asynchronous nature of the promises, var subscriptionName = response.data.name
could never happen, so the return is undefined
.
What I have tried:
I have tried a chained promise approach, but it does not behave the way I expect. Also, and even though it is not a good practice, I've tried to set up callbacks in the functions, but ultimately ending up in the same dead spot.
I'm pretty sure I can use an async function approach, but I'm concerned about compatibility issues.
Could you shed some light? Thank you.