Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API

From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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 -
@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. -
onclick="get_data()"
No need to overcomplicate since you ignore the resolved value or possible errors anyways -
@devRancid if its async it says it can't find the function. That was the first thing I tried.
edit:
async function get_data(){} -
@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. -
Or just onclick="get_data". It doesn't care about async, async is just sugar for new Promise((resolve) => ...)
-
@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> -
@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 -
@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> -
@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.
-
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. -
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!
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.
question
javascript
not equal?
fetch
xmlhttprequest