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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:53:40+00:00 2026-05-27T11:53:40+00:00

What’s the simplest way to define multiple jQuery.Callbacks() prerequisites? // simple pubsub example var

  • 0

What’s the simplest way to define multiple jQuery.Callbacks() prerequisites?

// simple pubsub example
var pubsub=(function() {
    var callbacksObj={};
    return function(id){
        if(!id) throw 'callbacks requires an id';
        return (callbacksObj[id] = callbacksObj[id] || $.Callbacks('unique memory'));
    };
})();
function fn1(){
    console.log('fn1');
};
function fn2(){
    console.log('fn2');
};
function fn3(){
    console.log('fn3');
};

// subscribing
pubsub('foo').add(fn1);
pubsub('bar').add(fn2);
pubsub('foo','bar').add(fn3); // adding a function with multiple dependencies

// publishing
pubsub('foo').fire() // should only log 'fn1';
pubsub('bar').fire() // should log both 'fn2' AND 'fn3' since both have fired

I can see wrapping each added function in another function that checks each id’s fired() state, though this seems like a common enough scenario that perhaps there’s a simpler way I’m missing.

  • 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-27T11:53:40+00:00Added an answer on May 27, 2026 at 11:53 am

    I think deferred is what you’re looking for:

    http://api.jquery.com/category/deferred-object/

    it looks like this:

    $.when(some_promise).then(some_callback)

    And you can have:

    $.when(some_promise, another_promise).then(some_callback)

    In this case some_callback will only be called if some_promise and another_promise have been resolved.

    deferred basically just adds a level of abstraction to your asynchronous functions, making it easier to express the dependencies. I suppose your example would look like:

    // simple pubsub example
    var pubsub=(function() {
        var callbacksObj={};
        return function(id){
            if(!id) throw 'callbacks requires an id';
            return some_assynchrnous_function(id); // like $.ajax
        };
    })();
    function fn1(){
        console.log('fn1');
    };
    function fn2(){
        console.log('fn2');
    };
    function fn3(){
        console.log('fn3');
    };
    
    // subscribing
    var foo = pubusb('foo');    // should return a deferred/promise
    var bar = pubsub('bar');
    
    $.when(foo).then(fn1);
    $.when(bar).then(fn2);
    $.when(foo, bar).then(fn3);
    

    I’m not entirely sure if this is correct for jQuery, but you get the idea. I didn’t find the jQuery API to make very much sense to me so I wrote my own :3

    I find it useful to be able to make ’empty’ deferred objects, then attaching a done handler to it, then passing the deferred object along to something that will eventually end up resolving it. I’m not sure if jQuery can do this.

    It may seem a little daunting at first, but if you can wrap your head around it you can get so much awesomeness from this. Dependencies is a big one but scoping is also great, you can add multiple done handlers on multiple levels, one handler may handle the actual data, one handler may just be interested in when the handler finishes so you can show a loading bar etc.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.