Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

IRT-fbachmann
Copy link
Contributor

No description provided.

Instead of requiring a callback function, saga handlers may also be synchronous or return a Promise.
@adrai
Copy link
Contributor

adrai commented Sep 12, 2017

Thanks for your valueable PRs...
But for these cqrs modules I don't want to support Promises... at least not in this major version 😉
I would design the whole thing a bit differently...
But thanks anyway...

@OrH
Copy link

OrH commented May 17, 2018

Is there a specific reason to not support Promises? Is it a wrong design or something?

@nanov
Copy link
Contributor

nanov commented May 17, 2018

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 saga.commit(callback); at the end.

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.

@OrH
Copy link

OrH commented May 17, 2018

Ok,
I needed to get some data from a denormalizer in a saga for enriching the next command, but currently it's not possible because the saga handler can't be async.
Or maybe you have another approach for this?

BTW,
The cqrs-swissknife library seems very interesting!

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 =]

@nanov
Copy link
Contributor

nanov commented May 18, 2018

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);
});

@OrH
Copy link

OrH commented May 19, 2018

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants