I am kinda new to Node.JS and am trying to write a "synchronous" loop in Node.JS, which in theory should do as follow:
- Loop over an object array, in order
- Apply a function on each object (actually creates the object in the DB and returns its unique id)
- After the first object has been created, use its id as parent_id for all other objects.
Problem is, I am lost between async/sync nature of callbacks and functions. E.g. ids.push won't give the expected result.
Edit: additionally, I am currently bound to Node 6.9 because of project constraints.
Code is tentatively as follows:
    function processAll( objectArray, callback ) {
        let ids = [];
        // Loop on all data
        objectArray.forEach( ( obj ) => {
            // From the second iteration on, 
            // objects are children of first obj 
            if( ids.length ) {
                obj.parent_id = ids[0];
            }
            someLib.doSomething( obj, ( err, result ) => {
                if( err ) {
                    return callback( err );
                }
                // This won't work, of course
                ids.push( result );
            });
        });
        return callback( null, ids );
    }
 
     
     
     
     
    