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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:24:33+00:00 2026-06-04T00:24:33+00:00

I have the following issue: Let’s say that I have a function that process

  • 0

I have the following issue:

Let’s say that I have a function that process an array to do something with it depending on received parameters. So something like:

 var arr = [];

 function processArr(p1, p2, ....){};

p1, p2 etc are received from the server so we get to:

processArr(<?php echo $p1; ?>, <?php echo $p2; ?>, ...)
processArr(<?php echo $p1; ?>, <?php echo $p2; ?>, ...)
processArr(<?php echo $p1; ?>, <?php echo $p2; ?>, ...)
processArr(<?php echo $p1; ?>, <?php echo $p2; ?>, ...)
processArr(<?php echo $p1; ?>, <?php echo $p2; ?>, ...)

It is actually a php for but doesn’t really matter.

Problem: js process the calls simultaneously (not exactly simultaneously but close enough) instead of one after the other. So if in first call I add an element to the array (then some other processing) and in second call I try to delete the element, at delete the element doesn’t exist because it wasn’t added yet.

How do I make second call to wait for the first to finish?

Added function:

    function processSaved(act, params)
    {
        if (act == 1)
        {
            var newUser = params;
            if (user.id == newUser.id)
                user = clone(newUser);
            activeElementIndex = i;
            // Go to the next level from the active element level
            var newLevel = newUser.level;

            // Set current level
            currentLevel = Math.max(currentLevel, newLevel);

            // Create new object and push it to elements array
            var obj = newUser;
            elements.push(obj);
            activeElementIndex = newUser.parent;
            // Add element to order list
            if (orderElements.length + 1 > newLevel)
            {
                var added = 0;
                for (var i = 0; i < orderElements[newLevel - 1].el.length; i++)
                {
                    if (elements[activeElementIndex].column < elements[elements[orderElements[newLevel - 1].el[i]].parent].column)
                    {
                        orderElements[newLevel - 1].el.splice(i, 0, elements.length - 1);
                        added = 1;
                        break;
                    }
                }

                if (added == 0)
                    orderElements[newLevel - 1].el.push(elements.length - 1);
            }
            else
            {
                var tmp = new Array();
                tmp.push(elements.length - 1);
                var obj = {"el": tmp};
                orderElements[newLevel - 1] = obj;
            }

            flagCreate = 1;
            actions.push(newUser);
            // Call the rearange function
            rearangeElementsWithoutRedraw();
        }
        else if (act == 0)
        {
            activeElementIndex = params.index;
            deleteNode();
        }

        return true;                                                
    }

First call is made with act = 1. Second is made with act = 0. If I add a timeout to the second call with let’s say 0.5 seconds everything works fine. If not I get error at removal because element does not exist. This suggests that the second call is made before first finished.

  • 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-04T00:24:35+00:00Added an answer on June 4, 2026 at 12:24 am

    JS does not call functions in parallel. Your processArr functions are executed sequentially.

    This obviously assumes that you do not simply start e.g. an AJAX request in the function – if you do the next function will obviously not wait for the request to finish (until it’s synchronous).

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

Sidebar

Related Questions

Let us say we have some code that looks like below: @interface SomeClass :
Folks, Let's say that I have the following situation: <div class=outer> <div> <span> <ul>
Let's say I have the following HTML: <ul> <li id=1>First issue.</li> <li id=2 class=error>Second
I have the following issue related to iterating over an associative array of strings
I have the following issue and I would like to know what exactly happens.
I have the following issue: I have a splash like window coming with my
I have got the following issue, my function appends code to a string $string
I have the following code: Function TruncateString(str, n) ' Returns an array with strings
Let's say I have the following structure in my ASP.NET MVC 3 application. Items
I'm developing a shared library. Let's say I have the following class definition: class

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.