So, I have a function which does some asynchronous work.
The function currently looks like this ( setTimeout
currently is implemented because I haven't managed to implement the async
/ await
way correctly):
function gatherAllRelevantReservationData(TimeFrameStartFromInputfield, TimeFrameEndFromInputfield, liebraum, liebsitz, reservationFromDatabaseTimeFramesStart, reservationFromDatabaseTimeFramesEnd){ console.log("reservationFromDatabaseTimeFramesStart inside gatherAllRelevantReservationData are ", reservationFromDatabaseTimeFramesStart, reservationFromDatabaseTimeFramesEnd) //console.log("TimeFrameStartFromInputfield and TimeFrameEndFromInputfield inside gatherAllRelevantReservationData are ", TimeFrameStartFromInputfield, TimeFrameEndFromInputfield) var timeFrameAsObject = convertDateStringToJavaScriptDateTimeObject(TimeFrameStartFromInputfield, TimeFrameEndFromInputfield) var startDateAsObject = timeFrameAsObject.start var endDateAsObject = timeFrameAsObject.end var timeFrameAsUnixTimeStamp = ConvertToCustomizedUnixTimestampString(startDateAsObject, endDateAsObject) var startDateAsUnixTimeStamp = timeFrameAsUnixTimeStamp.start; var endDateAsUnixTimeStamp = timeFrameAsUnixTimeStamp.end; getTable(startDateAsUnixTimeStamp, endDateAsUnixTimeStamp, liebraum); if(FavSeatcheckHasBeenEnabled == 1){ setTimeout(function(){ prepareSelectedAndDatabaseDateStringsForComparison(startDateAsObject, endDateAsObject, liebraum, liebsitz, reservationFromDatabaseTimeFramesStart, reservationFromDatabaseTimeFramesEnd); }, 300) } };
The getTable
function requires some time and needs to have finished before prepareSelectedAndDatabaseDateStringsForComparison
is called. The problem is that getTable
doesn't have any return value.
I'm pretty new to ES7 async
/ await
features as well as to ES6 promises. I know that await usually expects some promise to be returned, and I probably could arrange this in some very hacky, nasty way.
But I'd like to know if there is any other, elegant way around this.
Ideally, I'd just like to attach async
to gatherAllRelevantReservationData
and then put an "await" in front of getTable
call, but this of course did not work.
Any ideas how I could solve this?
EDIT: Here is the "getTable" function:
function getTable(start, ende, liebraum) { //console.log("start in getTable is " + start) //console.log("ende in getTable is " + ende) fillRooms(liebraum); $.post("../include/returnTable.php", { anfang: start, ende: ende, art: art }, function(data){ document.getElementById("tablediv").innerHTML= data; console.log("start inside callback of AJAX inside getTabel is ", start) //console.log("data after getTable function " + data); //fillRooms(liebraum); }) }
If you wish to use async
/ await
, you need to change getTable
to return a Promise
(or some thenable), because only they can be await
ed.
Fix your getTable
to return the $.post
call, so that its success can then be detected in gatherAllRelevantReservationData
function, and then you can simply await
the call of getTable
:
async function gatherAllRelevantReservationData(...) {
...
await getTable(...);
...
}
function getTable(start, ende, liebraum) {
fillRooms(liebraum);
return $.post("../include/returnTable.php", {
anfang: start,
ende: ende,
art: art
}, function(data) {
document.getElementById("tablediv").innerHTML = data;
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.