I'm building an app with Cordova. On one page, there is an EventListener that calls an async function. window.addEventListener("load", loadEdit(), false);
The function looks like this async function loadEdit(){...}
.
When testing in the browser I get the following error even though the function is fully executed:
TypeError: Property 'handleEvent' is not callable.
However, if I change the EventListener to another function which then calls the async function, there seems to be no problem. For example:
window.addEventListener("load", loadPre(), false);
...
function loadPre()
{
loadEdit();
}
Event handlers are not Dispatched Asynchronously So even though you're making an await call, the source thread is the Main UI thread. So you actually may still be blocking even though your event handler is async .
Using async functions with event handlers is problematic, because it can lead to an unhandled rejection in case of a thrown exception: const ee = new EventEmitter(); ee.
If you need to access an event in an asynchronous way, then you should call event. persist() at the beginning of the function.
An async function is a function declared with the async keyword, and the await keyword is permitted within it. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.
You can call an async function from your EventListener
.
The first problem I see is that you are invoking the callback function right away in the second argument of window.addEventListener
by including it as loadEdit()
instead of loadEdit
or () => loadEdit()
. You have to give it a function as second argument, right now you are giving a Promise or the return value of loadPre()
.
Try this way:
window.addEventListener("load", () => loadEdit(), false);
async function loadEdit() {
// do the await things here.
}
Async function return Promises. So, if you would like to do something after loadEdit
, try:
window.addEventListener("load", () => {
loadEdit().then(/* callback function here */);
}, false);
async function loadEdit() {
// do the await things here.
}
TL&DR: In order to use a call-back with an async function, you will need to decorate your async function within the Add Listener, and use await within the code block;
Explanation: As mentioned above in your example you are calling the function immediately; This means either two things will happen: Your function will return a value immediately OR you will get a promise; In either case its best to 'await' the call so the program runs more synchronously.
async function getStringSize(searchString) {
return searchString.length;
}
txtInput.addEventListener("keyup", async e => {
const searchString = e.target.value;
total_length = await getStringSize(searchString);
if (total_length > 3) {
console.log(searchString);
}
}, false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With