-2

I am working on a program that runs in ECMA 5.

All js files are use strict and this makes most suggestions online not usable as for example return new Promise.resolve is not usable in strict mode.

I am calling an async method to get an url and if a url is returned then the user should see a button that when pressed will forward the user to that page. I have this working with async and await. Now I want to move this from page load to do this after page is loaded so it doesn't affect page rendering. in case the service is down.

    var self = this;
    $(document).ready(function () {
        alert("ok");
        var promise = $.get(myService.getMyUrl());
        $.when.apply(self, promise).then(function() {
            self.url(arguments);
            self.showButton(self.url() ? true : false);
        });
    });

The alert gets shown so I know I'm getting here, but the problem is that I can't make the function an async function and simply await the result so how do I resolve the promise?

As stated above I already tried new Promise.resolve() (gives: using a variable without declaring it is not allowed in strict mode) and this code also does not work as arguments is not readable in strict mode. Any other ideas?

2 Answers2

0

From https://api.jquery.com/jquery.get/

jQuery.get( url [, data ] [, success ] [, dataType ] )

I think what you want is to put the success function into the call to get. But I don't know what self is supposed to refer to, and I think arguments is probably not being used correctly either. So I suggest setting them earlier.

  let self = something;
  let args = somethingElse;
  $(document).ready(function () {
      alert("ok");
      $.get(myService.getMyUrl(), function(data, textStatus) {
          // maybe set args from data?
          self.url(args);
          self.showButton(self.url() ? true : false);
      });
  });
Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • self = this; $(document).ready(function () { alert("ok"); $.get(myService.getMyUrl(), function (data, textStatus) { self.url(data); self.showButton(self.url() ? true : false); }); }); it does not get into the get, I've placed debugger; in there – Sebastiaan Haverkate Feb 18 '21 at 10:00
  • the arguments should be a result from when.apply according to an example I found. It should contain the results of the promise. – Sebastiaan Haverkate Feb 18 '21 at 10:07
  • Also maybe to clarify, the getMyUrl method is the async method that returns the url and that is all that is needed. You don't need to send a get request to that url. – Sebastiaan Haverkate Feb 18 '21 at 10:44
  • Then what you want is probably something like `myService.getMyUrl().then(function(url) {$.get(url);})`. But this is not made at all clear in your question – Chris Lear Feb 18 '21 at 11:05
0

Eventually it was solved with

    myService.getMyUrl().done(function(data) {
        self.myUrl(data);
        self.showButton(myUrl ? true : false);
    });

Apparently my question was unclear to some people. If things are unclear ask what is unclear as well as maybe read the question again slowly. Misinterpretation is clearly a thing.