Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between fork and spawn in redux-saga?

Tags:

redux-saga

The docs say fork is an attached fork and spawn is a detached fork - how do they differ?

like image 340
PaulB Avatar asked Apr 06 '17 15:04

PaulB


People also ask

What does fork do in redux-saga?

Attached forks (using fork )​ The fetchAll body itself terminates, this means all 3 effects are performed. Since fork effects are non blocking, the task will block on delay(1000) The 2 forked tasks terminate, i.e. after fetching the required resources and putting the corresponding receiveData actions.

What is difference between call and put in redux-saga?

What are the differences between call() and put() in redux-saga? Both call() and put() are effect creator functions. call() function is used to create effect description, which instructs middleware to call the promise. put() function creates an effect, which instructs middleware to dispatch an action to the store.

What happens if any of the forked task results in an error?

If any forked task raises an uncaught error, then the parent task will abort with the child Error, and the whole Parent's execution tree (i.e. forked tasks + the main task represented by the parent's body if it's still running) will be cancelled.

Can we use fork with race?

The fork effect always wins the race immediately. On the other hand, fork effects in a race effect is most likely a bug. In the above code, since fork effects are non-blocking, they will always win the race immediately.


1 Answers

One way to look at it is to see your saga's as a Graph. 'fork' creates a child from the calling process. While 'spawn' creates a new child at the root of the graph.

So when you 'fork' another process, the parent process will wait until the 'forked' process is finished. Also every exception will bubble up from the child to the parent and can be caught in the parent.

A 'spawned' process though will not block the parent, so the next 'yield' statement is called immediately. Also the parent process will not be able to catch any exceptions that happen in the 'spawned' process.

I hope this was helpful.

like image 123
Alex Avatar answered Sep 19 '22 05:09

Alex