Assuming I have a function
function* request(url) {
return global.fetch(url).then(response =>
_.result(response, 'json'))
}
Both code examples work fine to me
const {data} = yield call(request, 'http://example.com/api');
yield put(actionSuccess(data));
and
const {data} = yield request('http://example.com/api');
yield put(actionSuccess(data));
Therefore the question. What is an advantage of using the call effect for functions that return promises?
Some benefits of using call():
call() and other effects by directly supplying the return value.yield statement. These are the only points where cancellation can happen. A yield call() gives redux-saga an opportunity to cancel the task right before it's about to make that no longer needed call. Task cancellation is performed using gen.return() generator method. [1] [2]
yield, it's the only opportunity for redux-saga middleware to do any kind of scheduling other tasks. (I'm not sure how good redux-saga is at this, though.)For more information, I think it's best to open an issue on redux-saga's Github to ask the maintainer directly.
UPD: As this answer is getting attention: think twice before using redux-saga. It provides:
async/await available since ES7If you want to use Redux, I recommend opting for something simpler, like async thunks. You can have channels with async-csp and proper cancellation with cancellationtoken.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With