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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:53:13+00:00 2026-06-07T11:53:13+00:00

How can I correctly use jQuery deferreds to delay return of function until async

  • 0

How can I correctly use jQuery deferreds to delay return of function until async call within function complete + get return value?

This is my current code:

function getFields(page)
{
  var dff = $.Deferred();
  result = {};
  $.ajax( //the async call
    {
      url: page,
      success:
        function (data)
        {
          //work out values for field1 & field 2 from data here
          result = 
          {
            'field1' : field1,
            'field2' : field2
          };
        },
      complete:
        function()
        {
          dff.resolve(result); //my attempt to return the result
        }
    }
  );
  return dff.promise();
}

I want this to print {“field1″:”value1″,”field2″:”value2”}

var result = getFields('http://something');
console.log(JSON.stringify(result)); 

However, the value of result appears to be a jQuery object – so I’m doing something wrong, but what?

Thanks!


P.S. Sorry for the newbie question, I am a first time user of deferreds, so I am still grasping the basic concepts.

  • 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-07T11:53:15+00:00Added an answer on June 7, 2026 at 11:53 am

    The only way to delay the return of your getFields function would be to set the AJAX async property to false:

       var ajaxPromise = $.ajax(
        {
          url: page,
          async: false  // make the call synchronous
        }
      );
    

    But the jQuery documentation notes that this is deprecated from 1.8 onwards (i.e. it’s use is discouraged).

    Deferreds don’t make AJAX synchronous, instead they make it easier to work with callbacks and asynchronous methods.

    From what I can tell of what you’re trying to it might work better to do something like this:

    function getFields(page)
    {
      var ajaxPromise = $.ajax( //the async call
        {
          url: page
        }
      );
    
      var dff = $.Deferred();
      ajaxPromise.then(function(data){
    
        // Possibly access the loaded data in this function.
        var result = {
          'field1' : field1,
          'field2' : field2
        };
    
        // Notify listeners that the AJAX call completed successfully.
        dff.resolve(result);
    
      }, function(){
        // Something went wrong - notify listeners:
        dff.reject(/* possibly pass information about the error */);
      });
    
      return dff.promise();
    }
    

    Then use the promise object like this:

    var fieldPromise = getFields('http://something');
    fieldPromise.done(function(result){
      console.log(JSON.stringify(result)); 
    });
    

    Note that getFields returns a Promise object immediately but you have to wait for the promise to be resolved before you can log out the result.

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

Sidebar

Related Questions

I'm trying to use jQuery.post() function to retrieve some data. But i get no
Can someone help me with the correct use of the split function in perl
Solution Edit: Turns out you can't use the PHP SDK to return the correct
I'm trying to use jQuery with the Validate plugin, except I cannot get my
I was wondering if it would be possible to use jQuery's ajax function to
I'm trying to use one of my PHP functions within one of my jQuery
I'm having this issue and I can't seem to get my way. I use:
I can currently use sgml-pretty-print to pretty print an xml file in emacs, but
I currently use visual studio 2008 for creating projects that can run on windows.
Is there any way I can streamline my deployment process? I currently use these

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.