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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:23:01+00:00 2026-06-13T12:23:01+00:00

I need to run an arbitrary number of scripts. The next one can run

  • 0

I need to run an arbitrary number of scripts. The next one can run only of the previous has loaded and executed. I know RequireJS (and related) would be the correct choice, but I’m trying to learn about promises, so this is my experiment:

var files = [
  'first.js',
  'second.js',
  'third.js',
  'fourth.js'
];

var funcs = files.map(function(file) {
  return function() { return $.getScript(file); }
});

var deferred = $.Deferred();

funcs.reduce(function (soFar, f) {
   return soFar.then(f);
}, deferred.resolve(funcs[0])); 

Can someone elaborate about the pitfalls and alternatives to my solution?

  • 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-13T12:23:02+00:00Added an answer on June 13, 2026 at 12:23 pm

    What you’re really looking for is .pipe ( or in 1.8+, I believe .then was changed to mean the same thing )

    In short, pipe will allow you to chain promises in the way that you’re looking for. The code might look something like this ( untested ):

    var files, scriptsLoaded;
    
    files = [ 'first.js', 'second.js', 'third.js', 'fourth.js' ];
    
    while( files.length ) {
        (function() {
            var currentUrl = files.shift();
    
            scriptsLoaded = scriptsLoaded ?
                scriptsLoaded.pipe(function() {
                    return $.getScript( currentUrl );
                }) :
                $.getScript( currentUrl );
        }());
    }
    
    $.when( scriptsLoaded ).done(function() {
        // All scripts are now loaded assuming none of them failed
    });
    

    ** Edit **

    With that link you provided, I understand what you were trying to accomplish. Here’s a corrected version of your solution with some comments. It accomplishes the same thing as the other solution, however it’s a much more concise version:

    var files = [ 'first.js', 'second.js', 'third.js', 'fourth.js' ];
    
    // The initial value provided to the reduce function is a promise
    // that will resolve when the first file has been loaded.  For each
    // of the remaining file names in the array, pipe it through that first
    // promise so that the files are loaded in sequence ( chained ).
    // The value that is returned from the reduce function is a promise
    // that will resolve only when the entire chain is done loading.
    var scriptsLoaded = files.slice(1).reduce(function (soFar, file) {
        return soFar.pipe(function() {
            return $.getScript( file );
        });
    }, $.getScript( files[0] ); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need the ability to run an arbitrary command when I try and commit
I need to run two ffmpeg commands, one after the other i.e., wait until
I need to run an external application from within my Java code. I can
I need to process data that has a set of attributes where the number
I need a simple library or tool with which I can upload arbitrary files
i need run code that will create a database and populate tables. i am
I need run ts:reindex when smth add in model or destroy from model. How
I need to run the application that I have made in Xcode on my
I need to run application in every X seconds, so, as far as cron
I need to run a script that will generate the projects.list file when a

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.