Reputation: 45737
Hello I have this function:
function makeTweet(params) {
var tweetID = params.tweetID;
var tweetDate = params.tweetUrl;
var tweetText = params.tweetText;
var tweetUrl = params.tweetUrl;
var authorName = params.tweetDate;
var authorNickname = params.authorNickname;
var authorAvatar = params.authorAvatar;
if ( haveOauth() == true ) {
alert("We have the account, make the reply");
}else{
alert("We do not have it");
}
}
It should get the results of the below function and understand if it returns true or false and do a certain action based on that.
How do I do it? Whatever I do now, returns the same result. Something is wrong here. Thanks
function haveOauth() {
var user_id = "1";
var data = $.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
async: false,
success : function(data) {
if(data) {
return false;
}
}
});
}
Upvotes: 0
Views: 949
Reputation: 83356
async: false
is never recommended, but you could try replacing
if(data) {
return false;
}
with
var result = false;
var data = $.ajax({
// ...
success : function(data) {
result = data;
}
});
return result;
But why not have haveOath
take its own callback
function haveOauth(callback) {
var user_id = "1";
var data = $.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
success : function(data) {
callback(data);
}
});
}
And then
function makeTweet(params) {
var tweetID = params.tweetID;
var tweetDate = params.tweetUrl;
var tweetText = params.tweetText;
var tweetUrl = params.tweetUrl;
var authorName = params.tweetDate;
var authorNickname = params.authorNickname;
var authorAvatar = params.authorAvatar;
haveOauth(function(data) {
if (data) {
alert("We have the account, make the reply");
}else{
alert("We do not have it");
}
});
}
Upvotes: 1
Reputation: 5570
The function haveOauth
includes an AJAX call which is not recommended to be synchronous, as @AdamRackis pointed out. (Apart from that, your return
inside the callback function passed to $.ajax
does not do what you want, and you're missing a return true
somewhere – see the solution by @MichaelSwan.)
You could make your code asynchronous using a callback that gets passed the result of the check in haveOauth
:
function haveOauth(callback) {
var user_id = "1";
$.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
success : function(data) {
if (data) { // or whatever check needs to be made
callback(true); // signal that authenticated
} else
callback(false); // signal that not authenticated
}
});
}
And then use that function like this in makeTweet
:
haveOauth(function(isAuthenticated) {
if (isAuthenticated)
alert("We have the account, make the reply");
else
alert("We do not have it");
});
Upvotes: 0
Reputation: 82287
Similar to Michael Swan's answer - in that I don't think you can ever reach a situation where you return true.
function haveOauth() {
var user_id = "1";
var data = $.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
async: false,
success : function(data) {
if(data) {
return false;
}
}
});
return true;
}
Upvotes: 0
Reputation: 16764
Try this:
function haveOauth() {
var result = true;
var user_id = "1";
var data = $.ajax({
type: "POST",
url: "uspolitics_pulse/functions.php",
data: { type: 'checkOauth', user_id: user_id },
async: false,
success : function(data) {
if(data) {
result = false;
}
}
});
return result;
}
Upvotes: 0