3

I am trying to use Promise to make my code more readable and efficient but I'm stuck with the inability to get/set private attribute values. Consider the following example:

<aura:component>

    <aura:attribute name="message" type="String" access="private" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

The doInit logic:

({
    doInit: function(cmp, event, helper) {

        Promise.resolve(1)
            .then(function(message) {
                cmp.set("v.message", message);
                console.log(cmp.get("v.message"));
        });

I am unable to set or get a value for the message attribute. However, if I remove the access="private" property the code works.

I found a similar question here but according to the answer - if locker service was the problem - it should have been fixed already.

5

I answer my own question. I forgot to use $A.getCallback() to grant the correct access level to the asynchronous code.

The code should be:

Promise.resolve(1)
    .then($A.getCallback(function(message) {
        cmp.set("v.message", message);
        console.log(cmp.get("v.message"));
}));
  • 1
    Your post just saved me from removing access="private" from a bunch of components. Thanks! – Keith C Jan 12 '18 at 10:59
  • damn, I really need to remember putting that in my promises :D Think this is the 4th time that your post solves my problem – Nielsm Jun 6 '18 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.