reduxredux-sagacps

What does cps do in redux saga?


It's about 5 months that I work with redux-saga, it's a great and strong middleware library.

I know almost everything in redux-saga, but I couldn't still understand "cps".

Can anyone explain me (with an example) what can be done with "cps"?

I really appreciate if someone takes me out of this confusion.


Solution

  • cps effect is there to easily handle asynchronous functions that receive a nodejs style callback as last parameter.

    const doSomething = (param1, param2, callback) => {
      setTimeout(() => {
        callback(null, 'done')
      }, 1000)
    }
    
    function* saga() {
      const result = yield cps(doSomething, 'foo', 'bar')
      console.log(result) // 'done'
    }
    

    Documentation: https://redux-saga.js.org/docs/api/#cpsfn-args