redux-saga function dont send console log

1.2k Views Asked by At

I want to show on a generator function a console log statement but it doesnt work.

Why?

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

function* authFetch() {
  console.log('HI');
}

function* watcher() {
  while(true) {
    yield take("FETCH_REQUEST", authFetch);
  }
}

export default function* () {
  yield all([watcher()]);
}

If I edit my generator watcher function like this then it works:

function* watcher() {
  while(true) {
    yield take("FETCH_REQUEST", authFetch);
    yield call(authFetch, '');
  }
}

but I saw a lot of codes where there a no call only a take was there. Why my first code not works? I get no console.log('Hi');

1

There are 1 best solutions below

0
Nicholas Tower On
yield take("FETCH_REQUEST", authFetch);

take only expects one argument: the action type or pattern to wait for. So the second argument has no effect. take will pause until that action happens, and then it resumes your saga on the next line, which is where you put any code you want to run afterwards.

You may be thinking of other helper methods like takeEvery and takeLatest. For those, they will never move on to the next line, since they continue listening forever (unless cancelled). So for them, the second argument specifies which saga to fork when the action happens.