My await
for asyncCall()
to finish in the inputEntriesNotOnScreen
function does not work despite it being in an async function. I get this error:
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
What does the second part of the error mean?
function resolve1MS() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 1);
});
}
async function asyncCall() {
let clicked = false;
while( !clicked){
await resolve1MS();
if($('.ui-menu-item').length != 0){
$('.ui-menu-item')[0].click();
clicked = true;
$matrix = $("table.timesheet tbody tr");
return {
lastRow: $matrix.last()
}
}
}
}
async function inputEntriesNotOnScreen($matrix, notFoundInInitialSearch){
let lastRow = undefined;
notFoundInInitialSearch.forEach(function(item, index){
console.log(item, index);
if( lastRow == undefined){
lastRow = $matrix.last();
}
else{
lastRow = await asyncCall();
}
lastRow.find('.search')[0].click();
let $searchBar = $('.ui-autocomplete-input');multiple times
$($searchBar[index]).val(item[0][0]);
$searchBar[index].dispatchEvent(new Event('input'));
});
}