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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:41:22+00:00 2026-05-22T21:41:22+00:00

I am refactoring a resource-loading function that used a traditional callback pattern to instead

  • 0

I am refactoring a resource-loading function that used a traditional callback pattern to instead use jQuery Deferreds.

This function takes and array of urls, creates a new Deferred object for each resource, creates a $.when Deferred object to watch them, and returns the promise of the $.when object.

Here’s a simplified version of the method:

theLib = {
    getResources : function(paths) {
        var deferreds = [];
        theLib.isLoading = true;
        $.each(paths, function(i, path) {
            // do something with the path, either using getScript
            // or making a new $.Deferred as needed
            deferreds.push(/* the deferred object from above comment */);
        });
        theLib.currentDeferred = $.when.apply(null,deferreds).always(function() {
            theLib.isLoading = false;
        });

        return theLib.currentDeferred.promise();
};

This works well.

My problem: In the old script, not only would one call theLib.getResources() based on user actions or events, but one would also define a master list of resources that the application will “stream” while the user is not taking any action (i.e. reading an article).

Some of these streamed resources would be the same resources that could be called manually when a user does take action. The script was smart enough not to load a resource twice by keeping track of what was loaded.

It also keeps track of theLib.isLoading. The beginning of that function looked something like this:

getResources : function(paths, callback) {
    if (theLib.isLoading) {
        settimeout(function() {
            theLib.getResources(paths, callback);
        }, 100);
    }
    theLib.isLoading = true;

I can’t do this anymore, because I need to return a promise object.

I know that I can check theLib.currentDeferred.isResolved(). At that point, if it is not resolved: How can I add more deferred objects to the $.when queue that is being watched?

  • 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-22T21:41:23+00:00Added an answer on May 22, 2026 at 9:41 pm

    I guess I needed to ask the question to find a solution for myself. Basically I added the following code the beginning of getResources:

    if ( ! theLib.currentDeferred.isResolved()) {
    return $.when(theLib.currentDeferred).always(function() {
    theLib.getResources(paths);
    }).promise();
    }

    The above was failing. The correct solution was to pipe the results:

    if ( ! theLib.currentDeferred.isResolved()) {
        return theLib.currentDeferred.pipe(function() {
            return theLib.getResources(paths);
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This refactoring function in Eclipse for package names used to work for me fine
Whilst refactoring some old code I realised that a particular header file was full
While refactoring code and ridding myself of all those #defines that we're now taught
Whilst refactoring some legacy C++ code I found that I could potentially remove some
Whilst refactoring some code I came across some getter methods that returns a std::string.
I'm refactoring some PHP code and discovered that certain nested combinations of if ()
I'm working on an editor for files that are used by an important internal
I'm refactoring a large javascript document that I picked up from an open source
I'm refactoring an old script that does some operations on different sites. It needs
I'm refactoring a project that involves passing around a lot of arrays. Currently, each

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.