Redux-saga: How to create multiple calls / side effects programmatically for exit?

Using the reduction saga, you can simultaneously perform several effects:

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

// correct, effects will get executed in parallel
const [users, repos]  = yield [
  call(fetch, '/users'),
  call(fetch, '/repos')
]

How can I create these "call" calls programmatically ?

What I want to achieve:

Suppose I have an array with different parameters, and I want to execute a request to the server for one parameter, but in parallel with the sound reduction:

const parameters = ['abc', 'def', 'ghi']

const allMyFetchCalls  = parameters.map( (p) => makeCallRequestWithParameter(p) );

makeCallRequestWithParameter will create a function call (or in the case of reduction-saga-speech: effect) (selection, parameter), as in a yield call (selection, parameter)

const resultArray = yield allMyFetchCalls;

Is this possible, and if so, how?

+4
source share
2 answers

, call . Plain JavaScript . .

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

const params = ['abc', 'def', 'ghi']
const responses  = yield params.map(p => call(fetch, p))
+9

https://github.com/redux-saga/redux-saga/tree/master/docs/api#alleffects

function* mySaga() {
  const { customers, products } = yield all({
    customers: call(fetchCustomers),
    products: call(fetchProducts)
  })
}
+2

Source: https://habr.com/ru/post/1657759/


All Articles