Search code examples
node.jsfunctionreturnprocessingosc

Passing a return from one function to another function that already has set parameters?


Edit: I know JS is asynchronous, I have looked over the How to Return thread. The issue I'm having is that going from "foo" examples to something specific = I'm not quite sure where to re-format this.

Also here is some context: https://github.com/sharkwheels/beanballs/blob/master/bean-to-osc-two.js

I have a question about returns in node. It might be a dumb question, but here goes. I have a function that connects to a socket, and gets OSC messages from processing:

var sock = dgram.createSocket("udp4", function(msg, rinfo) {

    try {

        // get at all that info being sent out from Processing.

        //console.log(osc.fromBuffer(msg));

        var getMsg = osc.fromBuffer(msg);
        var isMsg = getMsg.args[0].value;
        var isName = getMsg.args[1].value;
        var isAdd = getMsg.address;
        var isType = getMsg.oscType;

        // make an array out of it

        var isAll = [];
        isAll.push(isName);
        isAll.push(isMsg);
        isAll.push(isAdd);
        isAll.push(isType);

        // return the array 
        console.log(isAll);
        return isAll; 


    } catch (error) {
        console.log(error);
    }
});

Below I have the start of another function, to write some of that array to a BLE device. It needs name and characteristics from a different function. How do I get the below function to use isAll AND two existing parameters?

var writeToChars = function (name, characteristics) { // this is passing values from the BLE setup function

    // i need to get isAll to here. 
    // eventually this will write some values from isAll into a scratch bank. 


}

Thanks.


Solution

  • Alright. So I figured out what to do, at least in this scenario. I'm sure there is a better way to do this, but for now, this works.

    I'm mapping an existing global array of peripherals into the write function, while passing the OSC message to it as a parameter. This solved my issue of "how do I get two pieces of information to the same place". It figures out which peripheral is which and writes a different value to each scratch bank of each peripheral accordingly. Leaving here for future reference.

    var writeToBean = function(passThrough){
    
    var passThrough = passThrough;
    
    console.log("in Write to bean: ", passThrough);
    
    _.map(beanArray, function(n){
        if(n.advertisement.localName === passThrough.name){
    
            //var name = n.advertisement.localName;
    
            n.discoverSomeServicesAndCharacteristics(['a495ff20c5b14b44b5121370f02d74de'], [scratchThr], function(error, services, characteristics){
    
                var service = services[0];
                var characteristic = characteristics[0];
                var toSend = passThrough.msg;
    
                console.log("service", service);
                console.log("characteristic", characteristic);
    
                if (toSend != null) {
                    characteristic.write(new Buffer([toSend]), false, function(error) {
                        if (error) { console.log(error); }
                            console.log("wrote " + toSend + " to scratch bank 3");
                    });
                }
    
                // not sure how to make the program resume, it stops here. No error, just stops processing. 
    
            });
        }       
    });
    }