7

I am trying to use javascript promises to perform multi-step asynchronous operations in lightning components. Unfortuantely I am stuck with the problem where the promise behaves incorrectly within $A.getCallback context. Here is a simplified app and js controller to demostrate the problem.

I have a simple app definition with a string attribute that defaults to "superman".

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

    <aura:attribute name="myatt" type="String" required="false" default="superman" access="private"  />
</aura:application>

Associated controller.js code uses Promises:

({
init : function( cmp, evt, h) {

    Promise.resolve(1).then($A.getCallback(function(val) {
            console.log('val %o', val);
            return new Promise($A.getCallback(function(done, err){
                setTimeout($A.getCallback(function(){
                    // console.log('now!');
                    done('yo ' + cmp.get('v.myatt'));
                }), 2000);
            }));
            // return 'yo ' + cmp.get('v.myatt');
    })).then($A.getCallback(function(res){
        console.log('Result: %o', res);
    }));
}
})

When locker service is disabled, the console log outputs an expected output (notice that Result appears after 2 seconds):

​val 1
Result: "yo superman"

However, when Locker Service is enabled, the Result becomes javascript Object with no useful attributes that is returned immediatly:

val 1
Result: Object
(When object is expanded)
-> $then$: function ()
-> __proto__: Object

I would really like to utilise the power of promises as it's a nightmare to use callbacks all the time. Any ideas?

Original question on sf dev forum, posting here as received no answer so far: https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XebSQAS

1 Answer 1

6

This is fixed internally. I've tried out your repro myself and see it working on my local. The fix is scheduled for the next major release - Spring '17.

Edit (Nov 15): We've decided to backport the fix to an upcoming patch. You can expect the fix to hit production the first couple days of next month (December).

5
  • Thanks @TrevorBliss . Could you please provide a bit more details about the patch? Specifically I would like to understand whether only Promises are affected or some other javascript functionality, too. Is there a web resource that would document the patches as I believe these sort of things don't make it into the release notes?
    – Artur
    Commented Nov 16, 2016 at 12:51
  • @Artur Each patch contains code from multiple teams that I do not track so I cannot guarantee no other javascript functionality will be affected, but the only code directly related to LockerService is a very straightforward fix to allow Promises to be used very similar to how they function outside of Locker. With the broken behavior, the framework is incorrectly trying to wrap the Promise objects and in the process losing the core functionality. With the fix, we bypass this incorrect wrapping logic and give you more direct access to the native Promise object. Commented Nov 17, 2016 at 0:06
  • And related to your release note question.. yes, a change like this is unlikely to make it in to the main release notes page. The only way to be informed of changes this small in patches would be to file a case and then when support links your case to our internal bug, you will get notified when the bug is fixed. I know this is not ideal and I'm starting discussions with folks internally about what else we could publicly expose for customers to get a digest of sorts for small patch changes like this. Commented Nov 17, 2016 at 0:09
  • For reference, you can keep an eye on the Release Notes changelog to stay up to date on bigger changes (this change almost certainly won't be included but a good link to bookmark IMO): docs.releasenotes.salesforce.com/en-us/winter17/release-notes/… Commented Nov 17, 2016 at 0:10
  • Thank you again for the detailed answer. I would definitely up-vote the idea of having the digest for small patches as that sort of changes make a big difference for me and quite likely most of the other developers.
    – Artur
    Commented Nov 17, 2016 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .