redux-saga:如何以编程方式创建多个调用/副作用以提高收益? [英] redux-saga: How to create multiple calls/side-effects programmatically for yield?

查看:98
本文介绍了redux-saga:如何以编程方式创建多个调用/副作用以提高收益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用redux-saga,可以并行执行多种效果:

With redux-saga, one can execute multiple effects in parallel:

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 those "call"-calls programmatically?

我要实现的目标是:

让我们说我有一个带有不同参数的数组,我想针对每个参数执行对服务器的请求,但要与redux-saga并行进行:

Lets say i have an array with different parameters and i want to execute a request to a server per parameter, but in parallel with redux-saga:

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

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

makeCallRequestWithParameter会创建一个函数调用(或在redux-saga-speech中:一种效果) call(fetch,param),就像在 yield call(fetch,param)中一​​样

makeCallRequestWithParameter would create a function call (or in redux-saga-speech: an effect) call(fetch, param) like in yield call(fetch, param)

const resultArray = yield allMyFetchCalls;

这可能吗?如果可以,怎么办?

Is this possible and if so, how?

推荐答案

请注意,call效果当时不会调用任何东西. 它只是创建普通的JavaScript对象并返回.因此,您想要的并不是那么困难.

Please note that call effect doesn't call anything at the time. It just creates Plain JavaScript Object and returns. So what you want is not so difficult.

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

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

这篇关于redux-saga:如何以编程方式创建多个调用/副作用以提高收益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆