I have the following JScode
function DetermineLoggedUser(){
return $.post('determineLoggedUser.php',{
}).then((result) => {
loggedUser = result;
})
The php looks like this:
<?php
session_start()
if(ISSET($_SESSION["loggedUser"])) {
echo $_SESSION["loggedUser"];
}else{
echo "'userNotLogged'";
}
?>
Now, I want DetermineLoggedUser()
to return the value of loggedUser
after it has been set by $.post AJAX call.
At the same time, I want the function calling DetermineLoggedUser()
to wait, using async/await.
So it would look kinda like this:
async function callingSeveralFunctions(){
//some functions
var result = await determineLoggedUser();
//some other functions which need to wait for determineLoggedUser()
}
function DetermineLoggedUser(){
return $.post('determineLoggedUser.php',{
}).then((result) => {
loggedUser = result;
})
callingSeveralFunctions();
So, since I need to return the promise created by the AJAX call in order to make "await" work, I wonder how I can at the same time return the value set to loggedUser
inside determineLoggedUser()
?
.then
handler (don't forget you can attach them at any time).