Reputation: 1174
My POST requests calls the same function twice and sends the data that I'm receiving from that function back to the client.
But I'm struggling on how to wait for both function calls to finish.
I tried using Promises but somehow it didn't work for me.
let socket_latest_data = new net.Socket();
let socket_optionmenu_data = new net.Socket();
app.post("/query_latest_data", function (req, res) {
let gauge_data = null;
let option_data = null;
let combined_data = {};
gauge_data = query_data(socket_latest_data, "d");
option_data = query_data(socket_optionmenu_data, "r");
Object.assign(combined_data, JSON.parse(gauge_data), JSON.parse(option_data));
res.end(combined_data);
});
function query_data(socket, char) {
socket.connect(2711, '192.168.1.173', function () {
socket.write(char);
});
socket.on('data', function (data) {
socket.removeAllListeners();
data = data.toString().replace(/[\[\]&]+|nan/g, '0').replace(/\s/g, '');
return data;
});
}
** UPDATE **
The solution is to use Promise.all()
Method. Its important to use .then(function()
part because otherwise i will just receive [undefined, undefined]
Code
app.post("/query_latest_data", function (req, res) {
let combined_data = {};
Promise.all([query_data(socket_latest_data, "d"), query_data(socket_optionmenu_data, "r")]).then(function(data) {
Object.assign(combined_data, JSON.parse(data[0]), JSON.parse(data[1]));
console.log(combined_data);
res.end(JSON.stringify(combined_data));
});
});
function query_data(socket, char) {
return new Promise((res, rej) => {
socket.connect(2711, '192.168.1.173', function () {
socket.write(char);
});
socket.on('data', function (data) {
socket.removeAllListeners();
data = data.toString().replace(/[\[\]&]+|nan/g, '0').replace(/\s/g, '');
res(data);
});
})
}
Upvotes: 0
Views: 137
Reputation: 12542
You can Make this a promise:
function query_data(socket, char) {
return new Promise((res, rej) => {
socket.connect(2711, '192.168.1.173', function () {
socket.write(char);
});
socket.on('data', function (data) {
socket.removeAllListeners();
data = data.toString().replace(/[\[\]&]+|nan/g, '0').replace(/\s/g, '');
res(data);
});
})
}
app.post("/query_latest_data", async (req, res) {
let combined_data = {};
let combined_data_arr = await Promise.all([query_data(socket_latest_data, "d"), query_data(socket_optionmenu_data, "r")])
Object.assign(combined_data, JSON.parse(combined_data_arr[0]), JSON.parse(combined_data_arr[1]));
res.end(combined_data);
});
Note: The code is not tested, given as a hint from top of my head.
Upvotes: 1
Reputation: 354
I think you can use callback in query_data function like below. If you want to use promise use async and await. Hope this works
app.post("/query_latest_data", function (req, res) {
let combined_data = {};
query_data(socket_latest_data, "d",(gauge_data)=>{
query_data(socket_optionmenu_data, "r",(option_data)=>{
Object.assign(combined_data, JSON.parse(gauge_data), JSON.parse(option_data));
res.end(combined_data);
});
});
});
function query_data(socket, char,callBack) {
socket.connect(2711, '192.168.1.173', function () {
socket.write(char);
});
socket.on('data', function (data) {
socket.removeAllListeners();
data = data.toString().replace(/[\[\]&]+|nan/g, '0').replace(/\s/g, '');
callBack(data);
});
}
Upvotes: 1