将 return 从一个函数传递到另一个已经设置参数的函数?
Passing a return from one function to another function that already has set parameters?
编辑:我知道 JS 是异步的,我查看了如何 Return 线程。我遇到的问题是从 "foo" 示例到特定的东西 = 我不太确定 在哪里 重新格式化它。
这里还有一些上下文:https://github.com/sharkwheels/beanballs/blob/master/bean-to-osc-two.js
我对节点中的 returns 有疑问。这可能是一个愚蠢的问题,但这里是。我有一个连接到套接字并从处理中获取 OSC 消息的函数:
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);
}
});
下面是另一个函数的开始,用于将该数组的一些内容写入 BLE 设备。它需要来自不同功能的名称和特征。如何让下面的函数使用 isAll AND 两个现有参数?
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.
}
谢谢。
本例中的异步调用可以这样写。如果需要,可以在闭包中的变量中维护状态。在这种特殊情况下 - 您也可以不使用任何状态 (isAll)。
var isAll;
var soc = dgram.createSocket('udp4', oncreatesocket);
function oncreatesocket(msg, rinfo)
{
isAll = parseMessage(msg);
writeData(isAll);
}
function parseMessage(msg) {
...
// code to parse msg and return isAll
}
function writeData() {}
如果writeData函数足够小。它可以在 oncreatesocket
内而不影响代码的可读性。
好的。所以我想出了该怎么做,至少在这种情况下是这样。我确信有更好的方法可以做到这一点,但就目前而言,这是可行的。
我正在将一个现有的全局外围设备数组映射到写入函数中,同时将 OSC 消息作为参数传递给它。这解决了我的 "how do I get two pieces of information to the same place" 问题。它确定哪个外围设备是哪个外围设备,并相应地将不同的值写入每个外围设备的每个临时存储区。留在这里以供将来参考。
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.
});
}
});
}
编辑:我知道 JS 是异步的,我查看了如何 Return 线程。我遇到的问题是从 "foo" 示例到特定的东西 = 我不太确定 在哪里 重新格式化它。
这里还有一些上下文:https://github.com/sharkwheels/beanballs/blob/master/bean-to-osc-two.js
我对节点中的 returns 有疑问。这可能是一个愚蠢的问题,但这里是。我有一个连接到套接字并从处理中获取 OSC 消息的函数:
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);
}
});
下面是另一个函数的开始,用于将该数组的一些内容写入 BLE 设备。它需要来自不同功能的名称和特征。如何让下面的函数使用 isAll AND 两个现有参数?
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.
}
谢谢。
本例中的异步调用可以这样写。如果需要,可以在闭包中的变量中维护状态。在这种特殊情况下 - 您也可以不使用任何状态 (isAll)。
var isAll;
var soc = dgram.createSocket('udp4', oncreatesocket);
function oncreatesocket(msg, rinfo)
{
isAll = parseMessage(msg);
writeData(isAll);
}
function parseMessage(msg) {
...
// code to parse msg and return isAll
}
function writeData() {}
如果writeData函数足够小。它可以在 oncreatesocket
内而不影响代码的可读性。
好的。所以我想出了该怎么做,至少在这种情况下是这样。我确信有更好的方法可以做到这一点,但就目前而言,这是可行的。
我正在将一个现有的全局外围设备数组映射到写入函数中,同时将 OSC 消息作为参数传递给它。这解决了我的 "how do I get two pieces of information to the same place" 问题。它确定哪个外围设备是哪个外围设备,并相应地将不同的值写入每个外围设备的每个临时存储区。留在这里以供将来参考。
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.
});
}
});
}