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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:21:29+00:00 2026-06-11T05:21:29+00:00

I have a simple single-dimension array, let’s say: fruits = [apples,bananas,oranges,peaches,plums]; I can loop

  • 0

I have a simple single-dimension array, let’s say:

fruits = ["apples","bananas","oranges","peaches","plums"];

I can loop through with with $.each() function:

$.each(fruits, function(index, fruit) {  
   showFruit(fruit);
});

but I’m calling another function which I need to finish before moving on to the next item.

So, if I have a function like this:

function showFruit(fruit){
    $.getScript('some/script.js',function(){
        // Do stuff
    })
}

What’s the best way to make sure the previous fruit has been appended before moving on?

  • 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-11T05:21:31+00:00Added an answer on June 11, 2026 at 5:21 am

    If you want one fruit to be appended before you load the next one, then you cannot structure your code the way you have. That’s because with asynchronous functions like $.getScript(), there is no way to make it wait until done before execution continues. It is possible to use a $.ajax() and set that to synchronous, but that is bad for the browser (it locks up the browser during the networking) so it is not recommended.

    Instead, you need to restructure your code to work asynchronously which means you can’t use a traditional for or .each() loop because they don’t iterate asynchronously.

    var fruits = ["apples","bananas","oranges","peaches","plums"];
    
    (function() {
        var index = 0;
    
        function loadFruit() {
            if (index < fruits.length) {
                var fruitToLoad = fruits[index];
                $.getScript('some/script.js',function(){
                    // Do stuff
                    ++index;
                    loadFruit();
                });
            }
        }
        loadFruit();
    
    })();
    

    In ES7 (or when transpiling ES7 code), you can also use async and await like this:

    var fruits = ["apples","bananas","oranges","peaches","plums"];
    
    (async function() {
        for (let fruitToLoad of fruits) {
            let s = await $.getScript('some/script.js');
            // do something with s and with fruitToLoad here
        }
    
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have an single dimension array (to keep it simple). Is there a
I have to reverse the elements of a simple (single-dimension) list. I know there's
I have a simple form with a single button and a datatable with a
I have a simple database named customer with a single table data.I want to
We have a simple WCF service that is tagged with InstanceContextMode = Single and
I have a simple producer/consumer scenario, where there is only ever a single item
I have a very simple WCF service running that has a single method that
I have a very simple OpenRasta app with a Home resource with a single
I have a single AMQ queue that receives simple messages with string body. Consider
I'm working on a relatively simple website with (currently) a single resource. I have

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.