Answer the question
In order to leave comments, you need to log in
How to apply the observer pattern correctly?
Hello. I ran into a problem when using the observer pattern. The situation is this: I receive a message from the user, and I need to do the following:
1) Get the time zone using an instance of the googleMapsAPI class
2) Get all previously recorded data in Redisconst zone = await geoTime.getTimeZone(msg.text)
const user = await redisApi.getUserForSave(msg.from.id)
const code = await sql.saveUser(user);
code === 'good' ? bot.sendMessage(msg.from.id, 'Good') : bot.sendMessage(msg.from.id, 'Good');
observer.on('end', (msg) => {
const zone = await geoTime.getTimeZone(msg.text);
const user = await redisApi.getUserForSave(msg.from.id);
const code = await sql.saveUser(user);
code === 'good' ? bot.sendMessage(msg.from.id, 'Good') : bot.sendMessage(msg.from.id, 'Good');
})
Answer the question
In order to leave comments, you need to log in
Since the message handler turns out to be cumbersome, it can be easily taken out into a separate module and imported like this.
const messageProcessor = require('path/to/message/processor');
// ваш код идет здесь ...
// далее навешиваем наш обработчик на шину сообщений
messageBus.on('end', messageProcessor);
// в вашем основном файле
messageProcessor.register(messageBus);
function messageProcessor() {
// ...
}
module.exports.register = function(messageBus) {
messageBus.on('end', messageProcessor);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question