I encountered a particular issue that states: "await is only valid in async function."
Here is the code snippet where the error occurs:
async function solve(){
var requestUrl = "url";
$.ajax({url: "requestUrl", success: function(result){
if(result.length < 3){
return false;
}else{
if(result.substring(0, 3) == "OK|"){
var ID = result.substring(3);
for(var i=0; i<24; i++){
var ansUrl = "url"+ID;
$.ajax({url: "ansUrl", success: function(ansresult){
if(ansresult.length < 3){
return ansresult;
}else{
if(ansresult.substring(0, 3) == "OK|"){
return ansresult;
}else if (ansresult != "ERROR"){
return ansresult;
}
}
}
});
await sleep(5000); // <-- ERROR
}
}else{
return ansresult;
}
}
},
fail: function(){
return "";
}
});
}
solve();
The initial function didn't have the 'async' keyword so I added it at the beginning but still faced the same error message. It's unclear whether the problem lies within the AJAX calls.