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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:14:10+00:00 2026-05-26T07:14:10+00:00

I have a standard javascript object whose prototype is extended with a .start() method

  • 0

I have a standard javascript object whose prototype is extended with a .start() method taking 2 callbacks as arguments: success and failure respectively. This method performs some asynchronous processing (it’s not AJAX) and depending on the result of this processing it invokes either the success or the failure callbacks.

Here’s how this could be schematized:

function MyObject() {
}

MyObject.prototype.start = function(successCallback, errorCallback) {
    (function(s, e) {
        window.setTimeout(function() {
            if (Math.random() < 0.8) {
                s();    
            } else {
                e();    
            }
        }, 2000);    
    })(successCallback, errorCallback);
}

It’s not really important the exact processing performed inside the method, only that it is asynchronous and non-blocking. I have no control over the point of time when the start method will finish the processing. I also have no control over the prototype and implementation of this method.

What I have control over is the success and failure callbacks. It is up to me to provide them.

Now I have an array of those objects:

var arr = [ new MyObject(), new MyObject(), new MyObject() ];

The order of elements in this array is important. I need to trigger the .start() method on each element of the array consecutively but only once the previous has completed (i.e. the success callback was called). And if an error occurs (the failure callback is called) I want to stop the execution and no longer invoke the .start method on the remaining elements of the array.

I could implement this naively by using a recursive function:

function doProcessing(array, index) {
    array[index++].start(function() {
        console.log('finished processing the ' + index + ' element');
        if (index < array.length) {
            doProcessing(array, index);
        }
    }, function() {
        console.log('some error ocurred');
    });
}

doProcessing(arr, 0);

This works fine but looking at the jQuery’s deferred Object that was introduced in jQuery 1.5 I think that there is a room for improvement of this code. Unfortunately I don’t feel very comfortable yet with it and I am trying to learn it.

So my question is is it possible to adapt my naive code and take advantage of this new API and if yes, could you provide me with some pointers?

Here’s a jsfiddle with my implementation.

  • 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-26T07:14:11+00:00Added an answer on May 26, 2026 at 7:14 am

    You could do something like this: (jsFiddle)

    function MyObject() {
    }
    
    MyObject.prototype.start = function(queue) {
        var deferred = $.Deferred();
        //only execute this when everything else in the queue has finished and succeeded
        $.when.apply(jQuery,queue).done(function() { 
            window.setTimeout(function() {
                if (Math.random() < 0.8) {
                    deferred.resolve();    
                } else {
                    deferred.reject();    
                }
            }, 2000); 
        });
        return deferred;
    }
    
    var arr = [ new MyObject(), new MyObject(), new MyObject() ];
    
    var queue = new Array();
    $.each(arr, function(index, value) {
        queue.push(value.start(queue)
            .done(function() {
               console.log('succeeded ' + index);
            })
            .fail(function() {
               console.log('failed ' + index);
            }));
    });
    

    Not quite sure wether you would consider this an improvement, though.

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

Sidebar

Related Questions

Like the Delicious submission bookmark-let, I'd like to have some standard JavaScript I can
I have a JavaScript widget which provides standard extension points. One of them is
Have you tried using straight javascript (rather straight ECMAscript 3 standard) in Flex applications,
Can you have custom client-side javascript Validation for standard ASP.NET Web Form Validators? For
I have a custom Javascript object that has a few string and float members.
Suppose I have a Javascript object which is initialized var letters = {q:0, t:0,
I have a php string containing the serialization of a javascript object : $string
I have a couple of standard ASP.NET web methods that I'm calling from javascript
The arguments object in JavaScript is an odd wart—it acts just like an array
Many languages have standard repositories where people donate useful libraries that they want others

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.