node-cqrs-saga icon indicating copy to clipboard operation
node-cqrs-saga copied to clipboard

Saga handlers may be synchronous or return Promises instead of using callbacks

Open IRT-fbachmann opened this issue 8 years ago • 6 comments

IRT-fbachmann avatar Sep 12 '17 16:09 IRT-fbachmann

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...

adrai avatar Sep 12 '17 16:09 adrai

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

OrH avatar May 17 '18 13:05 OrH

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.

nanov avatar May 17 '18 14:05 nanov

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

OrH avatar May 17 '18 15:05 OrH

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

nanov avatar May 18 '18 05:05 nanov

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!

OrH avatar May 19 '18 14:05 OrH