-1

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.

xabi_sides
  • 335
  • 3
  • 20
  • 4
    `getSubscriptionName` needs a `return` – Daniel A. White Mar 07 '18 at 11:45
  • 2
    `getSubscriptionName` doesn't return anything. You need to `return ServiceService.getService(...)`, which makes it return a promise, and then you need to chain that promise in `transformDetailValuesReworked` with `setFieldTransformation(iteration).then(function (fieldSetTransformation) { ... })`. – deceze Mar 07 '18 at 11:47

1 Answers1

0

You've simply forgotten to return anything from getSubscriptionName

function getSubscriptionName(subscriptionCode) {
        return ServiceService.getService(subscriptionCode)
          .then(function(response){
             var subscriptionName = response.data.name
             return response.data.name;
        })
    }

But then your next method needs to treat it as async too

function transformDetailValuesReworked(iteration) {
    //...
    var fieldSetTransformation = setFieldTransformation(iteration);
    return fieldSetTransformation; // This is a promise not a value!
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Given how the function is used in `transformDetailValuesReworked`, I don't think they forgot so much as *not understood* how promises work… – deceze Mar 07 '18 at 11:48
  • @deceze I got that too - updated but the dupe is the right one! – Jamiec Mar 07 '18 at 11:49
  • Hello, and thanks for the answers. I'm trying the Promise object approach: `function getSubscriptionName(subscriptionCode) { var subscriptionName; return new Promise(function(resolve, reject){ ServiceService.getService(subscriptionCode) }); }`, however this is not working. What I'm missing? – xabi_sides Mar 07 '18 at 13:03
  • 1
    @wickedchild you dont need to do that at all - just add the return statement as per my answer. But in answer to that direct comment - you're not `resolve`ing the promise. – Jamiec Mar 07 '18 at 13:52