-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Saga handlers may be synchronous or return Promises instead of using callbacks #41
base: master
Are you sure you want to change the base?
Conversation
Instead of requiring a callback function, saga handlers may also be synchronous or return a Promise.
Thanks for your valueable PRs... |
Is there a specific reason to not support Promises? Is it a wrong design or something? |
I guess the reason is compatibility with older node versions, and general design of the current library version. Is there any specific reason to support those ? No one stops you from implanting an async ( sugar for Promise ) saga handler, and calling commit when done. Also, the sagas require you to call Side note: we are currently developing an on-top library exactly for those cases, when one would like to use modern node features. It is in early stages, and documentation is missing, but it could serve you as an example how one could implement own solutions on top of this library. |
Ok, BTW, EDIT: just to clarify - It's possible to create a promise and call the commit within the promise as you mentioned, but my intention was for using async await =] |
Something like this: const { promisify } = require('util');
const myCollection = require('../path/to/my/collection');
const myCollectionFind = promisify(myCollection.find.bind(myCollection));
require('cqrs-saga').defineSaga({
name: 'myEvent',
aggregate: 'myAgg',
context: 'myCtx',
id: 'aggregate.id',
}, async (event, saga, callback) => {
const otherReadModels = await myCollectionFind({ my: 'rule' });
otherReadModels.forEach(rm => saga.addCommandToSend({ name: 'notify', id: rm.id }));
return saga.commit(callback);
}); |
I actually tried it, and thought it was blocking the other Sagas from running, but I must have done something wrong because I tried it again now and it works perfect! Thanks! |
No description provided.