Probably more of a JS question than Zendesk but...



  • Bryan Flynn

    Hi Paul. You're right - this behavior is because of the asynchronous nature of JavaScript Promise calls.

    If you change that #3 line to something like this, you should get the expected output:

    testFunction(client).then((result) => console.log(APPDATA.tagsToCheck));

    I also recommend reading up on the JavaScript Promise feature -- it's very powerful but does have a learning curve to it:

    JavaScript Promises: an Introduction

    MDN JavaScript Promise

  • Paul Tarling

    Hey Bryan,


    Thanks very much indeed for this - I really appreciate it. So, I tried as mentioned and unfortunately this doesn't appear to be working - I'm actually getting an error stating that it "cannot read property then of undefined". I'm guessing this is because my function is purely setting a variable to contain the data returned by the promise - the function isn't actually returning a promise itself, so isn't "thenable" in this context.


    I think here-in is my problem. I am not used to asynchronous programming at all - I'm from the good old Visual Basic 6 days where objects and synchronicity were just not something you needed to worry about. *cue community chasing after me with pitchforks*


    Really all I want to do is pull these items of data out in to a variable so I can easily manipulate them and perform a number of comparisons. Am I thinking about this the wrong way? I have read the links you very kindly provided as part of my research in to this, and I'm just stumped at this point.


    Thank you so much,

  • Bryan Flynn

    Working with JavaScript promises can be tricky. I missed it the first time but in your above code you need to "return" the promise that's being created when you call testFunction. This makes the code I gave above work:

          var APPDATA = {
              ticket_id: "",
              tagsToCheck: "",
              tagMatchOutput: ""

          function testFunction(client) {

            return client.metadata().then(function(metadata) {
              console.log("#1", metadata.settings.tagsToCheck);
              APPDATA.tagsToCheck = metadata.settings.tagsToCheck;
              console.log("#2", APPDATA.tagsToCheck);
            }).catch(function(err) {
              console.log('error', err);


          $(function() {
            var client = ZAFClient.init();
            testFunction(client).then((result) => console.log("#3", APPDATA.tagsToCheck));


    I'm curious. What app is it that used to be available that you're trying to recreate?

  • Paul Tarling

    Thanks so much Bryan - this has not only solved my problem, but allowed me to complete creation of the App!


    I was trying to re-create an app called "Tag Warnings" which essentially scans a ticket for specific tags and, if a match is found, displays a warning in the App sidebar, which is entirely customisable. 


    We were using this to alert agents to customers that had active complaints, are making use of our hosted service, have a special support contract, or any number of other things. The visual warning it gave was really handy, and we've ended up missing a number of things since it disappeared from the App Marketplace. 


    Fortunately, with your help, re-building it was actually pretty easy and we've also been able to improve on a number of things - for example you could only have 10 "checks" made for tags in the previous app. We're now using multi-line setting boxes, so now are only limited by the 64KB that can go in to these fields.


    Not quite ready for the prime-time yet... there's not really much by the way of error handling in it, and we're not validating anything on settings, but I may well decide to publish this once it's ready :)

  • Bryan Flynn

    That's great news Paul. Thanks for the info. I'm glad things are moving again :-) Have a great weekend!

    Bryan Flynn | Developer Support Manager


Please sign in to leave a comment.

Powered by Zendesk