Skip to content

Instantly share code, notes, and snippets.

@jorgecuesta
Created July 5, 2017 03:57
Show Gist options
  • Save jorgecuesta/24b96a1cfb2d808ee7dbb58d48488abc to your computer and use it in GitHub Desktop.
Save jorgecuesta/24b96a1cfb2d808ee7dbb58d48488abc to your computer and use it in GitHub Desktop.
Hemera.js Promise support
const Hemera = require('nats-hemera');
const nats = require('nats').connect();
const hemera = new Hemera(nats, {logLevel: 'fatal'});
const actionWithPromiseSupport = function(hemera) {
const act = hemera.act;
return function(pattern, callback) {
if (callback || pattern.pubsub$) return act.call(hemera, pattern, callback);
return new Promise((resolve, reject) => {
act.call(hemera, pattern, (err, resp) => {
if (err) return reject(err);
resolve(resp);
});
});
};
};
const addWithPromiseSupport = function(hemera) {
const add = hemera.add;
return function(pattern, handler) {
const useReply = handler.length === 2;
// callback with just one parameter expect return a Promise instance.
// callback with two parameters use normal approach of reply interface.
if (useReply || pattern.pubsub$) {
return add.call(hemera, pattern, handler);
}
add.call(hemera, pattern, function(msg, reply) {
const resp = handler.call(hemera, msg);
if (resp instanceof Promise){
return resp.then(function(resp) {
reply(null, resp);
}).catch(function(err) {
reply(err);
});
}
});
};
};
hemera.act = actionWithPromiseSupport(hemera);
hemera.add = addWithPromiseSupport(hemera);
const RejectHandled = hemera.createError('RejectHandled');
hemera.ready(function() {
/*
-------------
add - section
-------------
*/
hemera.add({topic: 'math', cmd: 'add'}, function(msg, cb) {
console.log('Request', msg);
cb(null, msg.a + msg.b);
});
hemera.add({topic: 'math', cmd: 'minus'}, async function(msg) {
console.log('Request',msg);
return Promise.resolve(msg.a - msg.b);
});
hemera.add({
pubsub$: true,
topic: 'pubsub',
cmd: 'one-to-many',
a: {
type$: 'number',
},
b: {
type$: 'number',
},
}, function(msg) {
console.log('Request', 'one-to-many', msg);
});
hemera.add({
topic: 'pubsub',
cmd: 'one-to-one',
a: {
type$: 'number',
},
b: {
type$: 'number',
},
}, function(msg) {
console.log('Request', 'one-to-one', msg);
});
hemera.add({topic: 'return', cmd: 'error'}, async function(msg) {
console.log('Request', msg);
return Promise.reject(new RejectHandled('you wanna this'));
});
/*
-------------
act - section
-------------
*/
hemera.act({topic: 'math', cmd: 'add', a: 1, b: 2}, function(err, resp) {
console.log('Response', resp);
});
hemera.act({topic: 'math', cmd: 'minus', a: 2, b: 1})
.then(function(resp) {
console.log('Response', resp);
})
.catch(function(error) {
console.log('Non expecting an error', error);
});
hemera.act({topic: 'return', cmd: 'error'})
.then(function(resp) {
console.log('Non expecting a response', resp);
})
.catch(function(error) {
console.log('Expected to fail', error.name);
});
hemera.act({
pubsub$: true,
topic: 'pubsub',
cmd: 'one-to-many',
a: 2,
b: 3,
});
hemera.act({
pubsub$: true,
topic: 'pubsub',
cmd: 'one-to-one',
a: 6,
b: 2,
});
/*
* you could also use async/await of course
* uncomment code below if you use babel-node or node.js 8
*/
/*
const withAsyncAwait = async function(){
try {
const resp = await hemera.act({topic: 'math', cmd: 'add', a: 1, b: 2});
console.log('Result', resp);
} catch(e) {
console.trace('Non expecting an error', e.name);
}
try {
await hemera.act({topic: 'return', cmd: 'error'});
console.trace('Non expecting a response');
} catch(error) {
console.log('Expected to fail', error.name);
}
return Promise.resolve();
};
withAsyncAwait().then(function(){
console.log('withAsyncAwait working');
});
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment