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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:18:33+00:00 2026-06-10T10:18:33+00:00

I have several different JavaScript objects with methods that i want to keep separate.

  • 0

I have several different JavaScript objects with methods that i want to keep separate. However, I need some type of observer, callback, or plugin design so that I can trigger these methods at the correct time.

For example, A.Foo() should almost always run after B.Bar() – but I do not want to place a call to A.Foo() inside B.Bar() because there are those odd times it should not run after B.Bar(). That would be coupling my code and I know that’s bad.

I want to approach this problem as if B.Bar() is just doing it’s job and does not ever plan on knowing about A.Foo() or any other function that might want to tag along with it. In other words, plugin support for future developers.

How do you design modular, non-coupled code with observer or event based callbacks in Javascript?

  • 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-10T10:18:35+00:00Added an answer on June 10, 2026 at 10:18 am

    It depends whether your scenario is really event like in nature, or more of an asynchronous operation:

    Event Based

    Using something like microevent you could do the following. This pattern is really common and simple, so I suggest that at some point you have a go at implimenting this yourself, as it’s great from an understanding point of view.

    MicroEvent.mixin(A);
    
    A.Foo = function () {
      //Do stuff A needs to do, then:
      A.trigger('foo-complete');
    };
    
    //Somewhere nice and separate and decoupled
    A.bind('foo-complete', B.Bar);
    

    If you need to do more complex things like filter the events, map the events etc. Reactive Extensions (which are based of the C# libraries) are really powerful, but that’s a big library to load in if you don’t need the functionality.

    Async operation

    Using Callbacks

    This can be done using callbacks, which is great for fairly simple operations:

    A.Foo = function (cb) {
      //Do stuff A needs to do, then:
      if (cb) cb();
    };
    
    A.Foo(B.Bar);
    

    Using promises

    These initially look more complicated, but they are easilly composable, and have nice ways of handling errors built in.

    Using Q which is one of the more popular promise libraries (again, there are loads of these):

    A.Foo = function () {
      var def = Q.defer();
      setTimeout(function () {
        //Simulate async operation
        def.resolve();
      }, 1000);
      return def.promise;
    };
    
    A.Foo().then(B.Bar).end();
    

    Using control flow libraries

    Control flow libraries (arguably promises are a special case of this) aim to help you compose operations that are based on the callback system.

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

Sidebar

Related Questions

I have several different content type nodes (videos, image galleries, stories...) that I would
I have several different numbers in a group that range in sizes and would
I have several different lists I want to call. They all have the same
In my game application I have several different activities that all can be accessed
I have several objects of different kinds (different function names, different signatures) and I
I have several xml files with different node structure. I want to extract xml
I have several objects on a page and I want to perform an operation
I have several javascript files that during run-time get combined and minified. This is
I have a textarea that I want to paste different text formats into. What
I am building a page that is employing several different javascript elements and I

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.