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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:19:33+00:00 2026-06-06T02:19:33+00:00

The basic pattern is if X do something asynchronously else do something synchronously. For

  • 0

The basic pattern is if X do something asynchronously else do something synchronously. For example

if (varNotSet) {
    setVarAsynchronously(function(callback) {
        // process callback then...
        render(page, {'var': myVar});
    });
}
else
    render(page, {'var': myVar});

What’s bothering me is the following

render(page, {var: myVar});

since the same code is repeated. Is there some way that I can encapsulate that logic in a single place?

  • 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-06T02:19:34+00:00Added an answer on June 6, 2026 at 2:19 am

    If you are using jQuery, I highly recommend its build-in $.Deferred function which is available sincs version 1.5
    It is made precisely to help with issues like the one you wrote.

    Using jQuery, you would do this the following way:

    var def = new $.Deferred;
    var promise = def.promise();
    if (varNotSet) {
        setVarAsynchronously(function(callback) {
            def.resolve();
        });
    }
    else
        def.resolve();
    
    promise.done(function () {
        render(page, {'var': myVar});
    });
    

    If the deferred object’s promise was already resolved, its callback is run immediately, otherwise it runs when it is resolved. Later you can attach more callbacks to the promise with .done(), and all of them will be called immediately if the deferred object was resolved.
    (You don’t have to make a promise object from the deferred object for making callbacks, it is just an optional thing that allows you to separate setting the deferred’s status from being able to add callback listeners. Otherwise you can do everything on the deferred object.

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

Sidebar

Related Questions

I am playing with a basic implementation of the Query Object pattern (in quotes
Basic question here - I have many lines of code that look something like:
Basic question - is it possible to access the current Page from a static
hopefully this is a basic question about make pattern rules: I want to use
I am looking for a basic pattern where I can move some shared code.
My requirements: A user should be able to draw something by hand. Then after
I'm working on something that might benefit from a pattern like the following: public
I am writing a function php which has to do something like this: we
I'm not doing any fancy route patterns yet, just the basic controller, action, id
Basic question here. I started using ARC and am not sure if I should

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.