I am creating a module dependent on another async one. I'm trying to find the best way to expose my module so that the developer using it finds it intuitive.
Initially my module looked something like this:
var soap = require('soap');
var service = {};
soap.createClient('http://mysoapservice', function(err, client){
service.myMethod = function(obj, callback){
//dependency on soap module
client.soapMethod(obj, function(err, data){
//do something with the response
//call other methods etc...
var res = "processed "+data['name'];
callback(err, res);
});
};
});
module.exports = service;
Of course the problem here is that calling mymethod before the callback inside the module code is reached will throw an exception.
What would be the best pattern to use for such a module?
Is returning promises specific to other libraries (q) an acceptable scenario? or should I just return a callback for when the module is initialized and let the developer handle the rest?