I'm trying to wrap my head around Promises (a common theme, I know). I have the basic ideas down but I'm having trouble implementing them.
I'm trying to build an object the exposes a set of functions based on the contents of a directory. I'm using bluebird to promisify the fs library. Then read in the files of the dir, build the objects, and return the result.
var Promise = require('bluebird'),
fs = Promise.promisifyAll(require('fs'));
var services = {};
return fs.readdirAsync('./path/to/file/')
.each(function (filename) {
//trim off the file extension and assign the export function
services[filename.replace(/\.[^/.]+$/, "")] = function(request) {
request.esbOperation = filename;
otherFunctionCall(request);
}
})
.then(function() {
return {services: services};
})
.catch(function(err){
console.log(err);
});
I've tried a variety of things but the returned object usually looks like this:
Promise {
_bitField: 167772160,
_fulfillmentHandler0: undefined,
_rejectionHandler0:
{ services:
{ function1: [Function],
function2: [Function],
function3: [Function],
},
_promise0: undefined,
_receiver0: undefined }
How do I get the result in the fulfillment handler? How do I get it returning the resolved object instead of the Promise object (I've tried resolve/reject in various places but I'm doing it wrong)?