var auth = true;
fetch('ajax.php')
.then(function(response) {
return response.json();
})
.then(function(res) {
auth = false;
})
.catch(alert);
console.log(auth);
var wasAuth = new Promise(
function (resolve, reject) {
fetch('ajax.php')
.then(function(response) {
return response.json();
})
.then(function(res) {
resolve("Auth == true");
// OR
reject(new Error("Auth == false"));
})
.catch(alert);
}
);
var sell = function () {
wasAuth
.then(function (fulfilled) {
console.log(fulfilled); // Auth == true
})
.catch(function(error) {
console.log(error.message); // Auth == false
});
};
sell();
<?php
$myObj->user = "John";
$myObj->isAuth = "true";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
var auth = false;
fetch('ajax.php')
.then(function(response) {
return response.json();
})
.then(function(res) {
auth = res.isAuth;
console.log("response: " + auth);
})
.catch(alert);
console.log("document ready: " + auth);