1

I am doing call on a function like this:

const didRefresh = yield call(maybeRefresh);
console.log('call - maybeRefresh, didRefresh:', didRefresh); // i expect this to return before the fetch in refreshTask completes

And maybeRefresh is this:

function* maybeRefresh() {
    const shouldRefresh = true;

    if (shouldRefresh) {
        yield fork(refreshTask);
        console.log('did fork refresh task, should now return while refreshTask happens async');
    }

    const didRefresh = shouldRefresh;
    return didRefresh;
}

function* refreshTask() {
    console.log('will fetch');
    const res = yield call(fetch, 'http://www.blah.com');
    console.log('ok refresh done, status:', res.status); // however its not return until here
}

When I fork the refreshTask, I expect the yield call at top to return a value BEFORE the fetch happens. However even though "did fork" gets logged, it seems it hangs even though its ready to return. It hangs on return until the forked task refreshTask completes, then it returns. Is this how yield call works? It works for any async stuff started by the child to finish?

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 1
    Not sure whether it's a duplicate of this, but for sure my answer here covers the question in detail, please have a look [here](https://stackoverflow.com/a/48054800/2998898) – Karen Grigoryan May 19 '18 at 11:47
  • Thanks @Grigoryan you're correct! I didn't understand that sentence in the docs till now. Thank you! – Noitidart May 19 '18 at 18:47
  • Possible duplicate of [What's the difference between fork and spawn in redux-saga?](https://stackoverflow.com/questions/43259301/whats-the-difference-between-fork-and-spawn-in-redux-saga) – Noitidart Mar 18 '19 at 21:28

0 Answers0