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

  • Home
  • SEARCH
  • 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 9193055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:04:14+00:00 2026-06-17T21:04:14+00:00

Here is a javascript function: function getValue(key) { var value; switch(_options.myType){ case cookie: value

  • 0

Here is a javascript function:

  function getValue(key) {
    var value;
    switch(_options.myType){
      case "cookie":
        value = readFromCookie(key);
        break;
      case "localStorage":
        value = readFromLocalStorage(key);
        break;
      case "db":
        //return value from the server
        // how do I wait for the result?
        $.ajax({
            type: "GET",
            url: "123",
            data: { .... },
            success: function(data){
               value = data; 
            }
        });
        break;
    }
    return value;
  };

In case of sending ajax request, I need to wait untill the ajax request has been completed. How can I do it?

Not that I can’t leave the function without having the value calculated. Which means that I can’t place a function inside success: handler which will return the value later (or perhaps I don’t understand something?). So it must be calculated within the function getValue().

UPDATE

P.S. So how do refactor my code to be able to use callback inside success: handler? Here is the second part of the code:

MyClass123.prototype.myMethod = function(value) {
      //..............
      var var1 = this.getValue("key1");
      if (var1 == "123") { ... }
      else { .... }
      //..............
    }
  };
  • 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-17T21:04:16+00:00Added an answer on June 17, 2026 at 9:04 pm

    See follow-up below

    You could make the ajax request synchronous, but that’s generally a bad idea (and jQuery won’t support it much longer).

    Instead, make getValue accept a callback:

    function getValue(key, callback) {
       switch(_options.myType){
         case "cookie":
           setTimeout(function() {
               callback(readFromCookie(key));
           }, 0);
           break;
         case "localStorage":
           setTimeout(function() {
               callback(readFromLocalStorage(key));
           }, 0);
           break;
         case "db":
           //return value from the server
           // how do I wait for the result?
           $.ajax({
               type: "GET",
               url: "123",
               data: { .... },
               success: function(data){
                  callback(data);
               }
           });
           break;
       }
    }
    

    Note that I’m using setTimeout for the readFromCookie and readFromLocalStorage callbacks. Why? Because if getValue might return its value asynchronously, it should always do so. using a timeout of 0 asks the browser to do it as soon as possible once control yields back to the browser (although it’s usually clamped to no less than 5-10ms).


    Re your comment below:

    Have you read what I write? I said that I can’t do it for some reason.

    I missed that bit, but with all due respect, you can. It may require some refactoring of code, but you can do it, and should. This is how modern web applications work. If your structure doesn’t support it, you need to fix the structure. You can use async: false on the request as a temporary workaround until you can do it properly. (Hey, we’ve all been there and had to do things like that.) Note that it’s scheduled for retirement before too long (just jQuery’s support of it, you’ll still be able to use XHR directly to do synchronous requests).


    Re your updated question, here’s how you update that code:

    MyClass123.prototype.myMethod = function(value) {
      this.getState("key1", function(var1) {
        //                           ^--- Note the var becomes an arg
        if var1 == "123"{ ... }
        else { .... }
      });
    };
    

    Note how little it actually changes. The logic that used to follow the call to getState instead goes into the callback you pass into it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my Javascript formvalidator function: function companyName() { var companyName = document.forms[SRinfo][companyName].value; if
Here is the JavaScript function ChangeVol(id) { document.form.selectFS_devId.value = id; document.form.selectFS_currentNameSpace.value = ; document.form.submit();
I have a javascript function which I wants in PHP, Here is my JavaScript
I need to pass an url from asp.net load_page to flowplayer javascript function here:
Here i am calling a javascript function on a button click and i need
My javascript function looks like this - function updateSuccess(aEle) { //Here aEle doesn't have
Here I need to call a javascript function first and after some time I
Here's a brief rundown of my issue: I have a JavaScript function that gets
I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
Window.open javascript function is not working in Mozilla, but working in other browsers, here

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.