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 7652787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:51:34+00:00 2026-05-31T11:51:34+00:00

Possible Duplicate: How can I get jQuery to perform a synchronous, rather than asynchronous,

  • 0

Possible Duplicate:
How can I get jQuery to perform a synchronous, rather than asynchronous, AJAX request?
how to wait for an ajax call to return

Hear me out. I completely understand this code segment.

$.getJSON(someURL, function(data){
    //do something with my data
})
.success(function () {
    //Call what you want on success
})

This seems fine if I just need to take one action that is pretty static. However, what if I want to be less restricted, for instance this

function my_func(){
    $.getJSON(someURL, function(data){
        //do something with my data... like modify an array or the dom
    })
}

Now the driver

my_func();
//Now I want to call a function that relies on the data that my_func brought into the script.

Is there something wrong with the way I’m coding my script if I want to do it like this? Or am I just missing some awesome built in method?

  • 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-05-31T11:51:35+00:00Added an answer on May 31, 2026 at 11:51 am

    I see two possible jQuery-ish ways there.

    The first would be to use another callback that can be passed to my_func:

    function my_func(callback) {
        $.getJSON(someUrl, function(data) {
            // ...do stuff ...
            if ($.isFunction(callback)) { callback(data); }
        });
    }
    
    my_func(function(data) {
        // ..do more stuff..
    });
    

    The second way would be to create a custom event that gets triggered inside my_func and can be listened to from the outside:

    function my_func() {
        $.getJSON(someUrl, function(data) {
            $(document).triggerHandler('my_func:data-received', [data]);
        });
    }
    
    $(document).on('my_func:data-received', function(event, data) {
        // ...do stuff...
    });
    
    my_func();
    

    I strongly recommend using async: false only if it is absolutely necessary.


    Just another (very neat) way to deal with this is the jQuery.Deferred object:

    function my_func() {
        var d = new $.Deferred();
        $.getJSON(someUrl, function(data) {
            d.resolve(data);
        });
        return d;
    }
    
    my_func().done(function(data) {
        // ...do stuff...
    });
    

    Your function returns an object that allows to register callbacks. Within the function you then just need to make sure to call resolve to invoke all registered done callback handlers.

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

Sidebar

Related Questions

Possible Duplicate: Trying to get tag-it to work with an AJAX call How can
Possible Duplicate: Can I get more than 1000 records from a DirectorySearcher in Asp.Net?
Possible Duplicate: Where can I find a tutorial to get started learning jQuery? Considering
Possible Duplicate: Where can I find a tutorial to get started learning jQuery? I
Possible Duplicate: How to get a DOM Element from a JQuery Selector I can
Possible Duplicate: How can I get a list of all the defined variables in
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: How can I get an element's serialised HTML with PHP's DOMDocument? PHP
Possible Duplicate: Where can I get to download Java comm api? I'm looking for

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.