javascript - Callback or promise never is completed through EventEmitter -
i wrote simple event dispatcher application using node's eventemitter
i have many events , works fine except one, don't know why promises or callbacks never completed
this code dispatcher
var eventemitter = require('events').eventemitter, requiretree = require('require-tree'), _ = require('lodash'); module.exports = { init: function(options) { var self = this; _.extend(options, { excludes: [] }); if (!options.path) { throw new error('you must specify `path` option'); } self.emitter = self.emitter || new eventemitter(); // require files in directory requiretree(options.path, { filter: filtermodels, each: loadmodels }); function filtermodels(filename) { return options.excludes.indexof(filename) === -1; } function loadmodels(model) { object.keys(model).foreach(function(key) { self.emitter.on(key, model[key]); }); } return this; } };
when application starts call dispatcher
dispatcher.init({ path: __dirname + '/events' });
the event issues
events/send_emails.js
var mandrill = require('mandrill-api/mandrill').mandrill; modules.exports = { 'send:email': function() { var mandrill = new mandrill('apikey', true); var message = { html: '<h1>hello email</>', from_email: 'info@example.com', from_name: "example", subject: "example mail", to: [{ email: 'my@email.com' }], headers: { 'reply-to': "info@example.com" } }; var cb = function(a) { console.log("print some", a); } mandrill.messages.send({message: message, async: false, ip_poll: 'main pool'}, cb, cb); } };
when app emits send:mail
event, function called, print some
message never showed in console
i test function send:mail
in simple script
test/index.js
var events = require('./events'); events['send:email']();
calling command
$ node test/index.js
and works!!!! saw message print some
on console
i don't understand wrong code event dispatcher
i have other events, write files, store records on database , call others apis without fail
also, wrote rough method using superagent return promises
var request = require('superagent-bluebird-promise'); request .post('https://mandrillapp.com/api/1.0/messages/send.json') .send({message: ...}).then(..).catch(...);
the .then
nor .catch
methods never called
Comments
Post a Comment