0
# Basic Effects
1
2
Core effects for the most common saga operations: waiting for actions, dispatching actions, calling functions, and accessing state.
3
4
## Capabilities
5
6
### take
7
8
Creates an effect that instructs the middleware to wait for a specified action on the Store. The generator is suspended until an action matching the pattern is dispatched.
9
10
```typescript { .api }
11
/**
12
* Wait for a specified action on the Store
13
* @param pattern - Action pattern to match (string, function, array, or undefined for all)
14
* @returns TakeEffect that resolves with the matched action
15
*/
16
function take<A extends Action>(pattern?: ActionPattern<A>): TakeEffect;
17
18
/**
19
* Wait for message from a channel
20
* @param channel - Channel to take from
21
* @param multicastPattern - Optional pattern for multicast channels
22
* @returns ChannelTakeEffect that resolves with the message
23
*/
24
function take<T>(
25
channel: TakeableChannel<T>,
26
multicastPattern?: Pattern<T>
27
): ChannelTakeEffect<T>;
28
```
29
30
**Usage Examples:**
31
32
```typescript
33
import { take } from "redux-saga/effects";
34
35
function* watchActions() {
36
// Take any action
37
const action = yield take();
38
39
// Take specific action type
40
const loginAction = yield take('LOGIN_REQUEST');
41
42
// Take multiple action types
43
const authAction = yield take(['LOGIN_REQUEST', 'LOGOUT_REQUEST']);
44
45
// Take with predicate function
46
const userAction = yield take(action => action.type.startsWith('USER_'));
47
}
48
```
49
50
### takeMaybe
51
52
Same as `take()` but does not automatically terminate the saga on an `END` action. Instead, sagas blocked on takeMaybe will receive the `END` object.
53
54
```typescript { .api }
55
/**
56
* Like take() but doesn't terminate on END actions
57
* @param pattern - Action pattern to match
58
* @returns TakeEffect that can receive END without terminating
59
*/
60
function takeMaybe<A extends Action>(pattern?: ActionPattern<A>): TakeEffect;
61
62
function takeMaybe<T>(
63
channel: TakeableChannel<T>,
64
multicastPattern?: Pattern<T>
65
): ChannelTakeEffect<T>;
66
```
67
68
### put
69
70
Creates an effect that instructs the middleware to dispatch an action to the Store. This effect is non-blocking and errors bubble back into the saga.
71
72
```typescript { .api }
73
/**
74
* Dispatch an action to the Store (non-blocking)
75
* @param action - Action object to dispatch
76
* @returns PutEffect
77
*/
78
function put<A extends Action>(action: A): PutEffect<A>;
79
80
/**
81
* Put action into a channel
82
* @param channel - Channel to put into
83
* @param action - Message to put
84
* @returns ChannelPutEffect
85
*/
86
function put<T>(channel: PuttableChannel<T>, action: T | END): ChannelPutEffect<T>;
87
```
88
89
**Usage Examples:**
90
91
```typescript
92
import { put } from "redux-saga/effects";
93
94
function* loginSaga(action) {
95
try {
96
const user = yield call(api.login, action.payload);
97
// Dispatch success action
98
yield put({ type: 'LOGIN_SUCCESS', user });
99
} catch (error) {
100
// Dispatch error action
101
yield put({ type: 'LOGIN_FAILURE', error: error.message });
102
}
103
}
104
```
105
106
### putResolve
107
108
Just like `put()` but the effect is blocking (waits for promise resolution if returned from dispatch) and will bubble up errors from downstream.
109
110
```typescript { .api }
111
/**
112
* Dispatch an action to the Store (blocking, waits for resolution)
113
* @param action - Action object to dispatch
114
* @returns PutEffect that waits for dispatch completion
115
*/
116
function putResolve<A extends Action>(action: A): PutEffect<A>;
117
```
118
119
### call
120
121
Creates an effect that instructs the middleware to call a function with arguments. Handles Promises, Generator functions, and synchronous values.
122
123
```typescript { .api }
124
/**
125
* Call a function with arguments
126
* @param fn - Function to call (can be generator, async, or sync)
127
* @param args - Arguments to pass to the function
128
* @returns CallEffect that resolves with the function result
129
*/
130
function call<Fn extends (...args: any[]) => any>(
131
fn: Fn,
132
...args: Parameters<Fn>
133
): CallEffect<SagaReturnType<Fn>>;
134
135
/**
136
* Call method on context object
137
* @param ctxAndFnName - Array of [context, methodName]
138
* @param args - Arguments to pass to the method
139
* @returns CallEffect
140
*/
141
function call<Ctx extends { [P in Name]: (this: Ctx, ...args: any[]) => any }, Name extends string>(
142
ctxAndFnName: [Ctx, Name],
143
...args: Parameters<Ctx[Name]>
144
): CallEffect<SagaReturnType<Ctx[Name]>>;
145
146
/**
147
* Call with context and function as object properties
148
* @param ctxAndFnName - Object with context and fn properties
149
* @param args - Arguments to pass
150
* @returns CallEffect
151
*/
152
function call<Ctx extends { [P in Name]: (this: Ctx, ...args: any[]) => any }, Name extends string>(
153
ctxAndFnName: { context: Ctx; fn: Name },
154
...args: Parameters<Ctx[Name]>
155
): CallEffect<SagaReturnType<Ctx[Name]>>;
156
```
157
158
**Usage Examples:**
159
160
```typescript
161
import { call } from "redux-saga/effects";
162
163
function* fetchUserSaga(action) {
164
// Call async function
165
const user = yield call(fetch, `/api/users/${action.payload.id}`);
166
const userData = yield call([user, 'json']);
167
168
// Call with context
169
const result = yield call([api, 'getUser'], action.payload.id);
170
171
// Call generator function
172
const processed = yield call(processUserData, userData);
173
}
174
```
175
176
### apply
177
178
Alias for `call([context, fn], ...args)`. Convenient way to call methods on objects.
179
180
```typescript { .api }
181
/**
182
* Call method on object (alias for call([context, fn], ...args))
183
* @param ctx - Context object
184
* @param fnName - Method name
185
* @param args - Arguments array
186
* @returns CallEffect
187
*/
188
function apply<Ctx extends { [P in Name]: (this: Ctx, ...args: any[]) => any }, Name extends string>(
189
ctx: Ctx,
190
fnName: Name,
191
args: Parameters<Ctx[Name]>
192
): CallEffect<SagaReturnType<Ctx[Name]>>;
193
```
194
195
### cps
196
197
Creates an effect that instructs the middleware to invoke a function as a Node-style function (with callback as last parameter).
198
199
```typescript { .api }
200
/**
201
* Call Node-style function with callback
202
* @param fn - Function that takes callback as last parameter
203
* @param args - Arguments to pass before callback
204
* @returns CpsEffect
205
*/
206
function cps<Fn extends (...args: any[]) => any>(
207
fn: RequireCpsCallback<Fn>,
208
...args: CpsFunctionParameters<Fn>
209
): CpsEffect<ReturnType<Fn>>;
210
211
interface CpsCallback<R> {
212
(error: any, result: R): void;
213
cancel?(): void;
214
}
215
```
216
217
**Usage Examples:**
218
219
```typescript
220
import { cps } from "redux-saga/effects";
221
import fs from "fs";
222
223
function* readFileSaga() {
224
try {
225
// Call Node-style fs.readFile
226
const content = yield cps(fs.readFile, 'config.json', 'utf8');
227
console.log('File content:', content);
228
} catch (error) {
229
console.error('Failed to read file:', error);
230
}
231
}
232
```
233
234
### select
235
236
Creates an effect that instructs the middleware to invoke a selector on the current Store's state.
237
238
```typescript { .api }
239
/**
240
* Get current state or run selector on state
241
* @param selector - Optional selector function
242
* @param args - Additional arguments for selector
243
* @returns SelectEffect that resolves with selected state
244
*/
245
function select(): SelectEffect;
246
function select<Fn extends (state: any, ...args: any[]) => any>(
247
selector: Fn,
248
...args: Tail<Parameters<Fn>>
249
): SelectEffect;
250
```
251
252
**Usage Examples:**
253
254
```typescript
255
import { select } from "redux-saga/effects";
256
257
// Selectors
258
const getUser = (state) => state.user;
259
const getUserById = (state, id) => state.users.find(u => u.id === id);
260
261
function* userSaga() {
262
// Get entire state
263
const state = yield select();
264
265
// Use selector
266
const user = yield select(getUser);
267
268
// Selector with arguments
269
const specificUser = yield select(getUserById, 123);
270
}
271
```
272
273
### delay
274
275
Creates an effect that blocks execution for specified milliseconds and optionally returns a value.
276
277
```typescript { .api }
278
/**
279
* Block execution for specified milliseconds
280
* @param ms - Milliseconds to delay
281
* @param val - Optional value to return after delay
282
* @returns CallEffect that resolves after delay
283
*/
284
function delay<T = true>(ms: number, val?: T): CallEffect<T>;
285
```
286
287
**Usage Examples:**
288
289
```typescript
290
import { delay } from "redux-saga/effects";
291
292
function* pollingSaga() {
293
while (true) {
294
yield call(fetchData);
295
// Wait 5 seconds before next poll
296
yield delay(5000);
297
}
298
}
299
300
function* timeoutSaga() {
301
// Delay with custom return value
302
const result = yield delay(1000, 'timeout complete');
303
console.log(result); // 'timeout complete'
304
}
305
```
306
307
### getContext
308
309
Creates an effect that instructs the middleware to return a specific property from the saga's context object.
310
311
```typescript { .api }
312
/**
313
* Get a property from the saga's context
314
* @param prop - Property name to retrieve from context
315
* @returns GetContextEffect that resolves with the context property value
316
*/
317
function getContext(prop: string): GetContextEffect;
318
```
319
320
**Usage Examples:**
321
322
```typescript
323
import { getContext } from "redux-saga/effects";
324
325
function* authSaga() {
326
// Get API client from context
327
const apiClient = yield getContext('apiClient');
328
329
// Get user session
330
const session = yield getContext('session');
331
332
// Use context values
333
const user = yield call([apiClient, 'getCurrentUser'], session.token);
334
}
335
```
336
337
### setContext
338
339
Creates an effect that instructs the middleware to update the saga's context object with new properties.
340
341
```typescript { .api }
342
/**
343
* Update the saga's context with new properties
344
* @param props - Object containing properties to merge into context
345
* @returns SetContextEffect
346
*/
347
function setContext(props: object): SetContextEffect;
348
```
349
350
**Usage Examples:**
351
352
```typescript
353
import { setContext, getContext } from "redux-saga/effects";
354
355
function* setupSaga() {
356
// Initialize context
357
yield setContext({
358
apiClient: new ApiClient(),
359
retryCount: 0
360
});
361
362
// Update retry count later
363
const currentRetryCount = yield getContext('retryCount');
364
yield setContext({ retryCount: currentRetryCount + 1 });
365
}
366
```