0

Using the following code I call a function ("firstFunction") that generates an Array and returns the array.

async function doProcess() {
  const checkState = await firstFunction();
  console.log(checkState);
  console.log(checkState.length);
  return checkState;
}

Using console.log(checkState); I am able to print the data from the whole Array to the console.

enter image description here

When I try to access the values or array data, for example, console.log(checkState.length);, I get 0. What am I doing wrong here?

Edit [added "firstFunction"]:

 function firstFunction() {
            var array = [];
            var url3 = "/Home/CheckPrintService?printer=" + document.getElementById("printerName").value;
            $.get(url3, null, function (data3) {
                $("#msgPrinterName").html(data3);
                var str = $("#msgPrinterName")[0].innerText.toString();
                if (str.includes("ERROR CODE")) {
                    array.push(str);
                }
                //console.log($("#msgPrinterName")[0].innerText.toString());
            });
            var e = document.getElementById("ddlViewBy");
            var deviceType = e.options[e.selectedIndex].text;
            var url2 = "/Home/CheckIfValidIP?input=" + document.getElementById("ipAddress").value + "&type=" + deviceType;
            $.get(url2, null, function (data2) {
                $("#msgIPPort").html(data2);
                var str = $("#msgIPPort")[0].innerText.toString();
                if (str.includes("ERROR CODE")) {
                    array.push(str);
                }
            });
            var url = "/Home/CheckPrinter?printer=" + document.getElementById("printerName").value;
            $.get(url, null, function (data) {
                $("#msgPrintService").html(data);
                var str = $("#msgPrintService")[0].innerText.toString();
                if (str.includes("ERROR CODE")) {
                    array.push(str);
                }
            });
            return array;
        }
piguy
  • 516
  • 3
  • 10
  • 30
  • 2
    @deceze Is this even a duplicate? The results are awaited and should be available, while the referenced duplicate has a problem with synchronicity. – Caramiriel Oct 05 '18 at 09:09
  • @Caramiriel It's entirely possible for `firstFunction` to return an array, even asynchronously, which will still be updated later, "even more asynchronously". The fact that the console shows `[]` is a sign of that behaviour. – deceze Oct 05 '18 at 09:16
  • That makes sense, while the console first logs `[ ]` and then when you open it it actually shows you the items (and length=3). – Caramiriel Oct 05 '18 at 09:19
  • 1
    Yeah, so in a nutshell, the implementation of `firstFunction` is wrong. Obviously we don't see *how* exactly. – deceze Oct 05 '18 at 09:20
  • I added "firstFunction". Thanks for your help guys – piguy Oct 05 '18 at 09:21
  • Yeah, that's exactly the kind of thing I expected. You're not awaiting your asynchronous calls inside `firstFunction`. – deceze Oct 05 '18 at 09:22
  • @deceze Do you have a URL for me where I can read about my problem? Thank you! I am not quite sure what to google about – piguy Oct 05 '18 at 09:24
  • 1
    I just added some duplicates. You probably want to return a jQuery `.when` promise from your function. – deceze Oct 05 '18 at 09:25
  • @deceze thank you! – piguy Oct 05 '18 at 09:25

0 Answers0