# Function code Inside settings.js

**URL:** https://discourse.nodered.org/t/function-code-inside-settings-js/841
**Category:** General
**Created:** [15 June 2018 22:50 UTC](https://discourse.nodered.org/t/function-code-inside-settings-js/841 "2018-06-15T22:50:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![aco7342](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aco7342/32/649_2.png) [@aco7342](https://discourse.nodered.org/u/aco7342)
#### Post date: [15 June 2018 22:50 UTC](https://discourse.nodered.org/t/function-code-inside-settings-js/841/1 "2018-06-15T22:50:29Z")

</div>

Sorry for my poor english…  
I have a function created inside settings using promisses and i need return async msg. howto make work?

PortaPinPad = (msg,os) =\> {  
return new Promise((resolve, reject) =\> {  
SerialPort.list()  
.then(function(ports) {  
msg.portas = ports;  
global.portas = ports;  
var porta = “”; // ‘\\.\COM6’  
var fabricante = “”;  
var vendorid = “”  
var productid = “”  
if (os.platform() == “win32”) {  
porta = ‘\\.\’ + ports[0].comName;  
fabricante = ports[0].manufacturer  
vendorid = ports[0].vendorId  
productid =ports[0].productId  
var portaNum = porta.replace(/[^0-9]+/, ‘’);  
}  
msg.PPC\_FindPort= { portaNum : portaNum , porta : porta , fabricante:fabricante,vendorid:vendorid,productid:productid }  
global.PPC\_FindPort = { portaNum : portaNum , porta : porta , fabricante:fabricante,vendorid:vendorid,productid:productid }  
console.log("\nPlatform:%s porta:%s %s", os.platform(), porta,portaNum)  
//}  
console.log(‘serial port success’);  
resolve( msg );  
}).catch(function(err) {  
console.log(‘serial port error %s’,err);  
msg.portas = [];  
global.portas = [];  
resolve( msg );  
});  
})  
}

function FormataTx( buffer) {  
var CRC16 = require(“crc16”);  
if (buffer.substr(0,3) == “OPN”){  
[//console.dir](https://console.dir)(global.PPC\_FindPort)  
var fabricante = global.PPC\_FindPort.fabricante  
if (fabricante.indexOf(“INGENICO”) \> -1) {  
buffer = buffer.substr(0,3)  
}else {  
buffer += global.PPC\_FindPort.portaNum  
}  
}  
var bufferTX = buffer+"\x17"  
var crc = CRC16(bufferTX)  
var message = Buffer.from( “\x16”+bufferTX+" " );  
message.writeUInt16BE(crc, bufferTX.length+1)  
return message;  
}

function TesteNode(msg) {  
var os = require(‘os’);  
msg.message = “OPN0020”;  
var initializePromisePorta = PortaPinPad( msg,os );  
initializePromisePorta.then( function (result) {  
console.log("\n\n4906 %s %s" , result.PPC\_FindPort.porta , result.message)  
result.dadosTX = FormataTx(result.message);  
console.dir( result ); // \<\<\<\<\<\<\<\<\<\< I wish return this msg \>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>  
return result;  
});

}

///////////////////////////////////  
And I use this way…

var TesteNode = global.get(‘TesteNode’);  
TesteNode(msg);  
return msg;

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 June 2018 23:25 UTC](https://discourse.nodered.org/t/function-code-inside-settings-js/841/2 "2018-06-15T23:25:29Z")

</div>

The documentation [here](https://nodered.org/docs/writing-functions#sending-messages-asynchronously) shows how to send messages asynchronously from within a Function node.

---

<div class="post-metadata">

### Author: ![aco7342](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aco7342/32/649_2.png) [@aco7342](https://discourse.nodered.org/u/aco7342)
#### Post date: [27 June 2018 22:23 UTC](https://discourse.nodered.org/t/function-code-inside-settings-js/841/3 "2018-06-27T22:23:05Z")

</div>

Thanks a lot. My problem was sync/async code. It’s works now
