arguments et and instance, passed to the function which is promisified are not visible (seen as null) in the return new promise block. Note:
rp = require('request-promise');
Promise = require('bluebird');
.. and some of my own modules like urlLib are 'required' at the top. rp library is promisified request library... It works if I explicitly set et and instance variables inside the return statement. (see commented et and instance declarations).
module.exports.getEntityInstance = function (et, instance) {
return new Promise(function(resolve, reject) {
// explicit setting, to test, works fine.
// var et = "device", instance = "33";
if ((!et) || (!instance)) {
// ****** FAILING HERE *******
reject(new Error(utilLib.errorAsJSON ("Invalid et/instance arg")));
}
OPTS.url = base_url + et + "/" + instance;
rp(OPTS)
.then(function (data) {
resolve(data)
})
.catch(function (err) {
reject(err);
});
});
}
Here's the unadulterated code:
module.exports.em7GetEntityInstance = function (et, instance) {
log.info("----- et before promise:(" + et + ")");
log.info("----- instance before promise:(" + instance + ")");
return new Promise(function(resolve, reject) {
// var et = "device";
// var instance = "33";
// if null or undefined...
if (!et || !instance) {
log.error("---- et and/or instance args null. rejecting");
reject(new Error(utilLib.errorAsJSON ("getEntityInstace():Invalid entity type argument")));
}
var et = et.toLowerCase().trim();
var url = baseurl + et + "/" + instance
log.info("url:(" + url + ")");
OPTS.url = url;
rp(OPTS)
.then(function (data) {
log.debug ("success. invoking resolve()");
resolve(data)
})
.catch(function (err) {
log.error("+++ rp failed:(" + err + ")");
reject(err);
});
});
}
And the log statements (its a server process (no console.log). Using winston log module.
2016-05-05T15:44:06.866Z - info: ----- et before promise:(device)
2016-05-05T15:44:06.867Z - info: ----- instance before promise:(33)
2016-05-05T15:44:06.867Z - error: ---- et and/or instance args null. rejecting