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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:01:56+00:00 2026-06-04T07:01:56+00:00

I have 4 scripts which I want to call one after another through ajax.

  • 0

I have 4 scripts which I want to call one after another through ajax.

I want to call script_2 only after script_1 has completed execution.

It means first I want to call script_1 in ajax, display the result of script_1 execution in its respective div and then call second script, show its result in second div and so on.

Currently what I am doing is creating a new ajax call inside the onreadystatechange() function (in a nested way).

Below is the pseudo-code:

var script1_ajax = new XMLHttpRequest();

script1_ajax.onreadystatechange=function() {            
    if (script1_ajax.readyState==4 && script1_ajax.status==200) {

        document.getElementById('result').innerHTML = script1_ajax.responseText;
        //create second ajax request and call it
        //similarly create two more nested ajax calls and call it
    }
}

I don’t think this is the proper way of doing this.
Please suggest how to do this in a less complicated way.

  • 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-04T07:01:57+00:00Added an answer on June 4, 2026 at 7:01 am

    Likely your best option is using a Javascript library, for example jQuery. jQuery has the jQuery.Deferred() object which can be used to easily represent a promise (a future result) thus allowing easy chaining of function calls. Note that $.get(), for example, returns a Deferred (see the documentation below in the page). This article also has a nice solution and a working jsfiddle, but it quites diverges from the Deferred approach, which is simply a matter of

    function _get(url, params, success) {
      return function() {
        return $.get(url, params, success);
      }
    }
    
    $.get("http://host/1", {}, updateDiv)   // see the doc for $.get()
      .pipe(_get("http://host/2"))          // see $.Deferred() and pipe()
      .pipe(_get("http://host/3"))
      .pipe(_get("http://host/4"));
    

    If you don’t know jQuery, $.get(url, params, callback) is the way to make asynchronous HTTP GET requests

    Note: I updated the code, since deferred.then() requires a callback (I gave it a promise) and replaced then() with pipe(), which gives the intended behavior on current jQuery versions (actually in 1.8 it seems then() is an alias to the current pipe())

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

Sidebar

Related Questions

I have a Makefile from which I want to call another external bash script
I have once ajax call which does connect to database, sets one flag in
I have a Perl script which I want to run every 4 hours through
I have a makefile which calls some bash scripts and shell commands. One of
I have one big batch script which sets bunch of environment variables. I want
I have a form which on submit I want to run a script via
I have a few IPython scripts which have redundant functionality. I would like to
I have a couple of scripts for which the first part of them looks
I have a server which executes Python scripts from a certain directory path. Incidently
I already have code which lazy loads scripts on request. My issue now is

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.