Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 258
Amir Gorji Avatar asked Oct 24 '25 02:10

Amir Gorji


1 Answers

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

like image 193
Martin Kadlec Avatar answered Oct 26 '25 22:10

Martin Kadlec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!