Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

Blocking on a javascript Promise.

$
0
0

I've a PropertyList where editors can select a further list of items, and in Edit Mode, it displays multiple [object Object] output, so I created a Dojo script to inspect and replace that output with a page title. My problem is I'm using 

"epi-cms/core/PermanentLinkHelper"

which takes a URL (which I have already) and returns something like a javascript promise (perhaps dojo promise?).

define(["dojo/_base/array", "dojo/_base/declare","dojo/_base/lang", "dojo/Deferred", "epi/dependency","epi-cms/contentediting/editors/CollectionEditor","epi-cms/core/PermanentLinkHelper",
],
    function (
        array, declare, lang, Deferred, 
        dependency, CollectionEditor, PermanentLinkHelper
    ) {
        async function awaitResolveContentData(urlStr) {
            let result = await PermanentLinkHelper.getContent(urlStr);
            console.log("awaiting : " + JSON.stringify(result));
            return result;
        };
        return declare([CollectionEditor], {            
            _getGridDefinition: function () {
                var that = this;
                var result = this.inherited(arguments);
                // where 'sectionItems' is the problematic collumn
                result.sectionItems.formatter = function (value) {
                    console.log("before Resolve " + value[0].link);
                    var r = awaitResolveContentData(value[0].link);
                    console.log("after Resolve " + JSON.stringify(r));
                };
                return result;
            }
        });
    });

which then outputs....

:90 before Resolve /link/fe1e795f89a5435d9fecbf7fc84fc7f8.aspx
:92 after Resolve {}
:30 awaiting : {"isPreferredLanguageAvailable":true, [... other object data snipped ..]}

...which demonstrates the promise is resolving AFTER the value is actually required. the "awaiting" is returning all the data I need, but too late :-(

has anyone encountered this issue before? how do I block on this promise? all the javascript guru's tell me once you're in 'asynchronous' mode that javascript can't return to synchronous mode very easily, because even implementing a 'while' loop to wait for the promise would block the promise and wont give it an oppurtunity to resolve the promise anyway. So I presume I need to restructure my code somehow? any help would be appreciated.


Viewing all articles
Browse latest Browse all 8020

Trending Articles