yeey, I got it working, although I don’t know why :P
var NodeHelper = require("node_helper");
var request = require('request');
var Promise = require("promise");
var result = new Array();
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node_helper for module [" + this.name + "]");
},
// subclass socketNotificationReceived
socketNotificationReceived: function(notification, payload){
if (notification === 'get_values') {
this.getJSON(payload);
}
},
requestp: function(url, key, name) {
return new Promise(function (resolve, reject) {
request({url:url, method: 'GET'}, function (err, res, body) {
if (err) {
return reject(err);
} else if (res.statusCode !== 200) {
err = new Error("Unexpected status code: " + res.statusCode);
err.res = res;
return reject(err);
}
result.push({url: url, name: name, temp: body});
resolve(body);
});
});
},
getJSON: function(object) {
var self = this;
for (var key in object) {
//console.log(object[key]);
self.requestp(object[key].url, key, object[key].name).then(function (info) {
//console.log(info);
}, function (err) {
console.error("%s; %s", err.message, url);
console.log("%j", err.res.statusCode);
});
}
//console.log(result);
self.sendSocketNotification('received_values', result);
result = [];
}
});
I wasn’t able to add a “temp” property to the original object - it was actually added, but it did not want to work outside the .then function :(