redux-saga: call vs fork and join
Not that much as far as I can tell, but you can then cancel the task between the fork
and the join
.
const task = yield fork(api, arg);
if (someCondition) {
yield cancel(task);
}
const result = yield join(task);
// Now a no-op since `join` blocked for the task to finish
cancel(task);
The difference is way bigger when you use spawn
instead. It will create a detached fork that is not automatically canceled when the parent task is (for example).
In addition to @Pier's answer,
Both can be used to invoke normal and generator functions.
Also, fork(fn, ...args)
perform a non-blocking call on fn - while call(fn, ...args)
is blocking.
Example:
function* main() {
yield fork(someSaga);
console.log('this won't wait for the completion of someSaga');
}
function* main() {
yield call(someSaga);
console.log('this will wait for the completion of someSaga');
}
Here a useful example of fork.