I am trying to use axios to call a URL and return its data. That part is working, but the ordering is not working when used as a function call. I am sure it's something simple, but I am not used to JavaScript.
The code below has two node.warn
parts so that I can see what is being returned.
The one in the function node.warn(["Finally", r, e])
shows the correct data, the one after the function call node.warn(["HTTP", httpReq]);
is returned first, and shows httpReq
as being undefined. It's like the code is not waiting for the function call to execute.
function httpRequest(method, url, headers) {
var r = null;
var e = null;
switch (method) {
case "GET": {
axios.get(url)
.then(response => {
r = response.data;
})
.catch(error => {
e = error;
})
.finally(() => {
node.warn(["Finally", r, e])
return [r, e];
})
break;
}
case "POST": {
node.warn("POST");
break;
}
default: {
raiseError("No method defined", msg);
break;
}
}
}
With the function call of
this.on('input', function(msg, send, done) {
if ((this.action != "Log Off") && (!flow.sid || flow.sid == undefined)) {
var url = "http://www.example.com"
var httpReq = httpRequest("GET", url);
node.warn(["HTTP", httpReq]);
}
done();
})