Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:05:05+00:00 2026-06-04T10:05:05+00:00

I would like to return x || $.get. Or in other words, if x

  • 0

I would like to return x || $.get.

Or in other words, if x is true, then return x, else perform a GET call and return the value provided by the server.

My attempt is listed below (ideally, it would follow the return x || y format maybe with an anonymous function? instead of the if/then).

Problem is my return from my $.get function appears not to be what I expected.

Would appreciate an explanation of what is going on.

Thanks

$(function(){

  function test(x,y) {
    if(x==true) {return true;}
    else{
      //test.php is echo($_GET['y']==123);
      $.get('ajax.php',{'y':y},function (status) {return status;});
    }
  }

  alert(test(false,123));

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:05:06+00:00Added an answer on June 4, 2026 at 10:05 am

    If you’re using jQuery 1.5 or later, Deferred and Promise are your friend for this kind of thing. Any time you call AJAX calls what you get back are Promise objects which you can attach functions to via .done(), .fail(), and .then().

    However! As pointed out by this excellent intro to deferred/promise and all this stuff (http://www.erichynds.com/jquery/using-deferreds-in-jquery/), you can also use $.wait()’s ability to handle a value that isn’t a promise to automatically do caching. So code like this:

    $.when(getToken()).done(
      function (token) {
        // do something with the token, which may or may not have been 
        // retrieved from the remote service
      }
    );
    

    Can handle getting either a cached value back or a promise with no problem:

    function getToken() {
      // Return either the cached value or a jQuery Promise. If $.when() gets the
      // cached value it will immediately realize that you didn't give it a
      // promise and it will instead create a jQuery Deferred to return and
      // .resolve() it using the value it did get. Thus, either way what
      // comes out of the function is something .when() can deal with and call a function.
      if (this.cache["token"]) {
        return this.cache["token"];
      } else {
        return $.get(" ... some url ... ");
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a $.get() request which returns some HTML. I would like to manipulate
I would like to return the id of a newly created object for my
I would like to return an array of string in my web services I've
I have a method where I would like to return an object instance of
I have a function that I would like to return 0 if a certain
I have a script with a factory method that I would like to return
I have created a dictionary in Class A and would like to return the
Given the following XML, I would like to return all eventtitles where the eventtype
Given the following directory: string fullpath = C:\MyDir1\MyDir2\MyDir3; I would like to return MyDir3
I'm constructing a regex that is looking for dates. I would like to return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.