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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:09:17+00:00 2026-06-09T10:09:17+00:00

Update 2: I think this is a better explanation: I have two callbacks, and

  • 0

Update 2:
I think this is a better explanation:

I have two callbacks, and each is filling up an array. I want to merge these arrays somehow. The naive way to do it is to wait until the arrays are filled, then merge with a for loop. I can’t do that because there’s no real way to check if the callbacks are “done”.

The next step might be something like this: Each time an object is downloaded by a callback, it checks the merge array (called mixed), and sees if it contains a “partner” object. If it does: then merge into that partner object. If it doesn’t, then insert that object.

I’m worried about race conditions: callback 1 sees the array is empty, decides to insert into it, but then control switches to callback 2, which inserts first. Callback 1 should now merge instead of insert, but when control switches back, it inserts instead.

What could help is if I could make an atomic “check and insert/merge” block of code. Is there a way to do that?


Update 1:
Now with code, and more words!

Thanks for your response, everyone. I went and tried to simplify the code as much as I could.

Words:

I have an array called mixed. Mixed should end up with: all objects of type (A), and if they have analogue objects of type (B) (also known as “private”s), they should be merged.

That means using two functions that use callbacks – getAllA and getAllB. Right now, I call both, wait a bit manually, then run a for loop that does that merging.

What I need to do is edit my code so that the merging happens in callbacks. However, I can’t think of a way to do that merging in a way that doesn’t create a race condition.

Naively, I might want to first fill up mixed with objects of type A, then go iterate through the B objects and merge them as needed. However, I can’t do that because there’s no way to see if you’re “done” with on(“contact_added”) in firebase. What I think I want is a concurrent array structure, but I don’t know how to use a built-in one. I’m worried about making my own because I don’t know how to check to if I did it wrong. Race conditions are tricky like that.

Now, the code:
Here are the scripts you need to reference:

And here’s what I paste into the javascript console:

    //Note: private ~~ B
    //      public ~ A

    var me = 'IWwypRHWpDmydd30o-v';
    var mixed = new Array();
    var mixedNames = new Array();
    var privates = new Object();

    var callbackB = function(snapshot){
        child = snapshot.val();
        privates[snapshot.name()] = child;
    };

    var callbackA = function(snapshot){
        var listRef = snapshot.ref();
        listRef.on('child_added', function (snapshot2) {
        child = snapshot2.val();
            mixedNames.push(snapshot2.name());
            mixed.push(child);
      });
    };

    var getAllB = function (callback, ownerid){
        var priv = new Firebase('http://gamma.firebase.com/innermost/project/private/' + ownerid);
        priv.on('child_added', function (snapshot){
        callback(snapshot);
      });
    };

    function getAllA(callback) {
      var pub = new Firebase('http://gamma.firebase.com/innermost/project/public');
      pub.once('value', function (snapshot) {
        if (snapshot.val() === null) {
          alert('snapshot does not exist.');
        }
        callback(snapshot);
      });
    }

    getAllA(callbackA);
    getAllB(callbackB, me);

    //////wait

    for (b in privates){
        var index = $.inArray(b, mixedNames);
        if (index < 0){ //if there is no record with this name
            mixed.push(privates[b]);
            mixedNames.push(b);
        }
        else{
            var pub = mixed[index];
            var mutt = returnMixed(pub, privates[b]);
            mixed.splice(index,1,mutt);
        }
    };

What I want to do is move the logic from the for loop into the callbacks, because if you run the for loop right away, the arrays won’t be done downloading through the callbacks yet.


The old entry:

I have two callbacks accessing lists of data on Firebase.

Callback 1 gets objects of type A.
Callback 2 gets objects of type B.

My goal – have an array that properly merges objects of type A and B.

Most (but necessarily all) objects of type B have a “partner” object of type A.
A few objects of type A have a partner object of type B.

For each object pulled from Firebase, I want to see if their partner is in the array. If not, then insert. If so, then “merge” with the partner instead.

I can’t think of a way to do this without a concurrent data structure – but I don’t know how to do that in Javascript.

How can I either create that concurrent Array, or figure out a way to achieve my goal some other 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-09T10:09:18+00:00Added an answer on June 9, 2026 at 10:09 am
    <script src="underscore.js"></script> <!-- or the lib of your choice -->
    <script>
    
       function addWithPartner( value, id, type ) {
          var indexOfPartner = _.find(values, function(partner, i) {
             // logic for determining what a partner is goes here
             // for example: 
             return value.name == partner.name;
          });
    
          if( indexOfPartner >= 0 ) {
             merge( values, indexOfPartner, value );
          }
          else {
             values.push( value );
          }
       }
    
       function merge( list, index, value ) {
          // do whatever merge means here
          // for example:
          _.extend( list[index], value );
       }
    
       var FB = new Firebase(YOUR_URL);
       var values = [];
    
       FB.child('TYPEA').on('child_added', function(snapshot) {
          addWithPartner( snapshot.val(), snapshot.name(), 'typeA' );
       });
    
       FB.child('TYPEB').on('child_added', function(snapshot) {
          addWithPartner( snapshot.val(), snapshot.name(), 'typeB' );
       });
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update I don't think I was clear enough when I originally posted this quesion.
This SQL statement example is very close to what I think I need... update
I have this sql: UPDATE JOBMAKE SET WIP_STATUS='10sched1' WHERE JBT_TYPE IN (SELECT JBT_TYPE FROM
Backbone.js sents an POST on updated objects instead of an PUT. I think this
[2015 update: I think it's safe to say that Flash is rapidly dying. Don't
I think it is pretty straightforward. All I am trying to do is update
I need to perform update/insert simultaneously changing structure of incoming data. Think about Shops
I'm trying to better understand why one of our database update scripts failed to
I have created this file at My/View/Helper/FormElement.php <?php abstract class My_View_Helper_FormElement extends Zend_View_Helper_FormElement {
I have a multiple contexts running in one tomcat instance each context need access

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.