1

According to the doc:

we yield plain JavaScript Objects from the Generator. We call those Objects Effects.

it seems to be a JavaScript object looking e.g. like this:

// Effect -> call the function Api.fetch with `./products` as argument
{
  CALL: {
    fn: Api.fetch,
    args: ['./products']
  }
}

It seems to be a result of one of effect creators.

However documentation to all([...effects]) looks like accepted parameters are effects and at he same time all() function accepts promises and generators as well.

import { all, call } from 'redux-saga/effects'

const callEffect = call(console.log, 'foo')

function* gen() {
    console.log('bar')
}

const promise = new Promise(resolve => {
    console.log('baz')
    resolve()
})

function* print() {
    yield all([ callEffect, gen(), promise])
}

Calling print() saga will result in foo, bar and baz being printed.

What actually is an effect? Array of what type is parameter of all([...effects]) function?

1
  • When talking about effects in redux-saga you can mean either just the effect objects or anything that the redux-saga corutine can handle. Although generally I think people will mean the former. I think the type of the 'array' can be only described by 'what the authors decided to support` which can be found in redux-saga api reference. If you are interested in deeper understanding of effects in programming I recommend watching youtube.com/watch?v=z8SI7WBtlcA. Jun 5, 2018 at 21:44

1 Answer 1

1

An Effect in redux-saga is a simple object describing a side effect. The idea is, instead of directly executing the "effect" (which could be a regular function call, or a put, or a select, etc), you yield an Effect object describing the operation to redux-saga, and the library decides what to do with it (which, in the case of call, will just execute your function). The rationale for this is that it hypothetically makes testing easier, because you can run a saga as a normal generator and just compare the objects yielded, without having to mock anything.

The fact that Effects, Promises, and generators are mostly interchangeable in redux-saga is just a convenience feature of the library. Promises and generators are not themselves Effects, but they are used often enough in redux-saga code that the author saw benefit in supporting them as first-class citizens. For instance, yielding a Promise will block until the promise resolves, and doing yield call on a function that returns a Promise will do the same. This has nothing to do with effects, though, so the documentation is a bit misleading here. (If you look at the TypeScript definition for all, you can see that there is actually a generic version of it that takes parameters of any type, not just Effects.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.