<commit_before>define( [
    'Q'
], function ( Q ) {

    var osgWrapper = {};

    osgWrapper.Object = function ( input, obj ) {
        var jsonObj = input.getJSON();
        var check = function ( /*o*/) {
            return true;
        };
        if ( !check( jsonObj ) ) {
            return undefined;
        }

        if ( jsonObj.Name ) {
            obj.setName( jsonObj.Name );
        }

        if ( jsonObj.UserDataContainer ) {
            var userdata = input.setJSON( jsonObj.UserDataContainer ).readUserDataContainer();
            if ( userdata !== undefined ) {
                obj.setUserData( userdata );
            }
        }

        return obj;
    };

    osgWrapper.Node = function ( input, node ) {
        var jsonObj = input.getJSON();

        var check = function ( /*o*/) {
            return true;
        };
        if ( !check( jsonObj ) ) {
            return undefined;
        }

        osgWrapper.Object( input, node );

        var promiseArray = [];

        var createCallback = function ( jsonCallback ) {
            var promise = input.setJSON( jsonCallback ).readObject();
            var df = Q.defer();
            promiseArray.push( df.promise );
            Q.when( promise ).then( function ( cb ) {
                if ( cb ) {
                    node.addUpdateCallback( cb );
                }
                df.resolve();
            } );
        };

        if ( jsonObj.UpdateCallbacks ) {
            for ( var j = 0, l = jsonObj.UpdateCallbacks.length; j < l; j++ ) {
                createCallback( jsonObj.UpdateCallbacks[ j ] );
            }
        }

        if ( jsonObj.StateSet ) {
            var pp = input.setJSON( jsonObj.StateSet ).readObject();
            var df = Q.defer();
            promiseArray.push( df.promise );
            Q.when( pp ).then( function ( stateset ) {
                node.setStateSet( stateset );
                df.resolve();
            } );
        }

        var createChildren = function ( jsonChildren ) {
            var promise = input.setJSON( jsonChildren ).readObject();
            var df = Q.defer();
            Q.when( promise ).then( function ( obj ) {
                df.resolve( obj );
            } );
            return df.promise;
        };

        var queue = [];
        // For each url, create a function call and add it to the queue
        if ( jsonObj.Children ) {
            jsonObj.Children.forEach( function ( jsonChildren ) {
                queue.push( createChildren( jsonChildren ) );
            } );
        }

        // Resolve first updateCallbacks and stateset.
        var deferred = Q.defer();
        Q.all( promiseArray ).then( function () {
            deferred.resolve( );
        } );
        
        var defer = Q.defer();
        // Need to wait until the stateset and the all the callbacks are resolved
        Q.when (deferred.promise).then (function(){
            Q.all( queue ).then( function () {
                // All the results from Q.all are on the argument as an array
                // Now insert children in the right order
                for ( var i = 0; i < queue.length; i++ )
                    node.addChild( queue[ i ] );
                defer.resolve( node );
            } );
        });
        return defer.promise;
    };
<commit_msg>Fixed bad using of promises, Fix to avoid the use of for_each<commit_after>define( [
    'Q'
], function ( Q ) {

    var osgWrapper = {};

    osgWrapper.Object = function ( input, obj ) {
        var jsonObj = input.getJSON();
        var check = function ( /*o*/) {
            return true;
        };
        if ( !check( jsonObj ) ) {
            return undefined;
        }

        if ( jsonObj.Name ) {
            obj.setName( jsonObj.Name );
        }

        if ( jsonObj.UserDataContainer ) {
            var userdata = input.setJSON( jsonObj.UserDataContainer ).readUserDataContainer();
            if ( userdata !== undefined ) {
                obj.setUserData( userdata );
            }
        }

        return obj;
    };

    osgWrapper.Node = function ( input, node ) {
        var jsonObj = input.getJSON();

        var check = function ( /*o*/) {
            return true;
        };
        if ( !check( jsonObj ) ) {
            return undefined;
        }

        osgWrapper.Object( input, node );

        var promiseArray = [];

        var createCallback = function ( jsonCallback ) {
            var promise = input.setJSON( jsonCallback ).readObject();
            var df = Q.defer();
            promiseArray.push( df.promise );
            Q.when( promise ).then( function ( cb ) {
                if ( cb ) {
                    node.addUpdateCallback( cb );
                }
                df.resolve();
            } );
        };

        if ( jsonObj.UpdateCallbacks ) {
            for ( var j = 0, l = jsonObj.UpdateCallbacks.length; j < l; j++ ) {
                createCallback( jsonObj.UpdateCallbacks[ j ] );
            }
        }

        if ( jsonObj.StateSet ) {
            var pp = input.setJSON( jsonObj.StateSet ).readObject();
            var df = Q.defer();
            promiseArray.push( df.promise );
            Q.when( pp ).then( function ( stateset ) {
                node.setStateSet( stateset );
                df.resolve();
            } );
        }

        var createChildren = function ( jsonChildren ) {
            var promise = input.setJSON( jsonChildren ).readObject();
            var df = Q.defer();
            Q.when( promise ).then( function ( obj ) {
                df.resolve( obj );
            } );
            return df.promise;
        };

        var queue = [];
        // For each url, create a function call and add it to the queue
        if ( jsonObj.Children ) {
            for ( var i = 0, k = jsonObj.Children.length; i < k; i++ )
            {
                queue.push( createChildren( jsonObj.Children[ i ] ) );
            }
        }
        // Resolve first updateCallbacks and stateset.
        var deferred = Q.defer();
        Q.all( promiseArray ).then( function( ) {
            deferred.resolve( );
        } );
        
        var defer = Q.defer();
        // Need to wait until the stateset and the all the callbacks are resolved
        Q( deferred.promise ).then( function( ){
            Q.all( queue ).then( function( ) {
                // All the results from Q.all are on the argument as an array
                // Now insert children in the right order
                for ( var i = 0; i < queue.length; i++ )
                    node.addChild( queue[ i ] );
                defer.resolve( node );
            } );
        });
        return defer.promise;
    };

{'fraction_non_alphanumeric': 0.1150350171360453, 'fraction_numerical': 0.0007450454477723141, 'mean_word_length': 1.3281304197016996, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3504632', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1753, 'n_words': 491}