5

Oh the irony. I gotta ask a JS question here...

How come I can't do this?
<button type="button" id="idGetData1" onclick="{async()=>{ await get_data();}}">getDataFromWebserver</button>

But I have to do this?
document.getElementById("idGetData1").onclick=async() => {
await get_data();
};

Why the extra steps and need to run in onload?

Playing around with fetch instead of XMLHttpRequest for testing an embedded board.

Comments
  • 2
    I don't remember now but my 2 guesses would be

    can you run JavaScript in the onclick event? I guess so

    maybe you can't await though. maybe whatever runs the JavaScript isn't async
  • 2
    @jestdotty that is all I can think.

    I tried just calling the function and it doesn't recognize it if its async. So I guess I could do a promise inside a regular function maybe? I dunno. I am just doing it for feedback for embedded systems calls.
  • 5
    onclick="get_data()"
    No need to overcomplicate since you ignore the resolved value or possible errors anyways
  • 1
    @devRancid if its async it says it can't find the function. That was the first thing I tried.

    edit:

    async function get_data(){}
  • 1
    @devRancid so I found this answer on SO that talks about immediate invocation. My call was just creating the async object or something. It needs explicitly evoked:

    onclick="(async()=>get_data())();"

    SO question (second answer):

    https://stackoverflow.com/questions...

    Where the assignment of the async object gets invoked on onclick apparently. So this can be simpler. Nice.
  • 2
    Or just onclick="get_data". It doesn't care about async, async is just sugar for new Promise((resolve) => ...)
  • 0
    @BordedDev

    These do not work if the function is marked async (firefox):

    <button type="button" id="idGetData1" onclick="get_data">getDataFromWebserver</button>

    <button type="button" id="idGetData1" onclick="get_data();">getDataFromWebserver</button>
  • 2
    @Demolishun idk what you mean, works fine in chromium and firefox: https://jsfiddle.net/6aq4bhmv/

    And in one of your examples it's not being called
  • 1
    @Demolishun This works fine, check if you're not doing something that would cause get_data to be defined late or not in global?

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>Title</title>

    <script>

    async function get_data() {

    const response = await fetch(window.location);

    const data = await response.text();

    console.log(data);

    }

    </script>

    </head>

    <body>

    <button onclick="get_data()">Get Data</button>

    </body>

    </html>
  • 2
    @devRancid I am doing a fetch, so I dunno. Every time I try on the embedded system it fails. But your demo does work. Weird.
  • 2
    @BordedDev so I am testing again and now it works. Wtf...
  • 1
    I must have missed a step when testing before.

    I have to edit the js file, save, run converter to go from js file to embedded C file, compile, write to flash, restart board, test. Pain in the ass.
  • 1
    @Demolishun That does indeed sound like a big PITA
  • 0
    I think I found the original problem. If for some reason a function doesn't compile it will obviously not be callable. The issue was I was not seeing the error in the log in the browser. DOH!
Add Comment