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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:55:11+00:00 2026-06-17T08:55:11+00:00

I am trying to force a serialized logic onto a set of asynchronous activities

  • 0

I am trying to force a serialized logic onto a set of asynchronous activities on a webpage. I am fairly certain I want to use the jQuery deferred object, but I run into the problem that the functions I want to execute are dependent on when the user decides to make selections by clicking various buttons. I am looking for help doing this using the following jsFiddle idea:

Consider a sequence of 4 buttons. When clicked, each button disables itself and enables the next button. Each button shouldn’t have its event set until after it has been enabled. There’s an added task (in this case an alert) to be done ONLY AFTER the 3rd button has been enabled.

Basic HTML Code

<button id="btn1">Click 1st</button>
<button id="btn2" disabled="disabled">Click 2nd</button>
<button id="btn3" disabled="disabled">Click 3rd</button>
<button id="btn4" disabled="disabled">Click 4th</button>

Tasks In Each Step

var fnDoStageOne = function() {
  $("#btn1").one("click", function(event, ui) {
    $("#btn1").prop("disabled", true);
    $("#btn2").prop("disabled", false);
    //STAGE ONE IS ONLY DONE AFTER THIS POINT
  });
};

var fnDoStageTwo = function() {
  $("#btn2").one("click", function(event, ui) {
    $("#btn2").prop("disabled", true);
    $("#btn3").prop("disabled", false);
    //STAGE TWO IS ONLY DONE AFTER THIS POINT
  });
};

var fnDoStageThree = function() {
  $("#btn3").one("click", function(event, ui) {
    $("#btn3").prop("disabled", true);
    $("#btn4").prop("disabled", false);
    //STAGE THREE IS ONLY DONE AFTER THIS POINT
  });
  alert("Shouldn't see this if button 3 isn't active yet");
};

var fnDoStageFour = function() {
  $("#btn4").one("click", function(event, ui) {
    $("#btn4").prop("disabled", true);
    alert("Task complete");
    //STAGE FOUR IS ONLY DONE AFTER THIS POINT
  });
};

Incorrect Control Logic

var oDeferredObj = $.Deferred();

oDeferredObj.then(fnDoStageOne);
oDeferredObj.then(fnDoStageTwo);
oDeferredObj.then(fnDoStageThree);
oDeferredObj.then(fnDoStageFour);
oDeferredObj.resolve();

The jsfiddle can be seen here: http://jsfiddle.net/sva79/

My initial understanding was that I could just chain the functions into the deferred with the .then() function. Obviously, this doesn’t work as the additional task in step 3 triggers on page load. How would I need to adjust the control or logic of this scenario to put of resolving each step until the appropriate button press has been registered?

  • 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-17T08:55:12+00:00Added an answer on June 17, 2026 at 8:55 am

    Your code sample doesn’t do what you think it does. All you are doing is adding to the list of things to be done directly after oDeferredObj is resolved. Furthermore, there’s the issue of resolving when a task is actually “done”, which your code doesn’t quite label.

    This seems like something I’ve addressed before in another question some time ago, but I’m not sure I would like the answer I gave back then, so let me try anew.

    What you are seeking is a way to chain new promises together. You also want, I presume, a way to say when promises are resolved (or rejected).

    A good way to take data asynchronously from one promise to another is to chain them with pipe, but as you want to trigger the completion of a task from within a UI event, I’m having trouble imagining something better than what I have below.

    I won’t swear that this is the best way to do it, the simplest way I can think of to handle this is to create a utility function that takes a given promise and a “task”, creates a new promise, lets your task decide what to do with the promise, but only after the given promise is resolved, and return the new promise.

    var nextStage = function (promise, task) {
      var oDeferredObj = $.Deferred();
      promise.then(function () {
        task(oDeferredObj);
      });
      return oDeferredObj;
    }
    

    This then can be used to daisy chain your tasks together:

    /* Creation of deferred and initial wiring */
    var p1;
    var starterObj = $.Deferred();
    p1 = starterObj;
    p1 = nextStage(p1, fnDoStageOne);
    p1 = nextStage(p1, fnDoStageTwo);
    p1 = nextStage(p1, fnDoStageThree);
    p1 = nextStage(p1, fnDoStageFour);
    p1.done(function () {
      alert("All stages done.");
    })
    $("#start").one("click", function (event, ui) {
      //fire it up.
      $("#start").prop("disabled", true);
    });
    

    And obviously, you’ll want to do something to signal that a particular task is done at some point, for example:

    var fnDoStageOne = function (promise) {
      //setup, etc.
      $("#btn1").one("click", function (event, ui) {
        //.. whatever needs to happen  in the ui.
        //STAGE ONE IS ONLY DONE AFTER THIS POINT
        promise.resolve();
      });
    };
    

    Mind you, I’ve only given you a start for when promises succeed. It’d be wise do do a bit more for when they fail. Also, if you want to pass data from one promise to another, you may desire need to pipe, etc.

    Full source (modified from yours) in a jsFiddle here

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

Sidebar

Related Questions

I'm trying to force an inherited class to use a custom attribute. I'm creating
I'm trying to force ssl on certain pages, but also on a directory. But
i'm trying to force a div to have a certain height depending on his
I'm trying to force Webkit to use proxy definde in APN. My proxy configuration:
I'm trying to force Magento to display a certain block only when a configuration
I'm trying to force Jetty to only use HTTPS (or redirect to HTTPS from
I am trying to force users to only select certain values when adding a
I'm trying to force a download of a protected zip file (I don't want
I'm trying force BigDecimal to use symbols (Ex. Instead of Decimal.multiply, do Decimal *),
I'm trying to force myself to use as little mouse as possible 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.