VariableTao
VariableTao

Reputation: 171

Threads in Redux Saga

I am a new Redux Saga learner, and I am confused that if JavaScript is single thread why there are many threads in Redux Saga? Some resource state that they are not actually threads,but I am wondering why they are called threads and what them are actually.

Upvotes: 1

Views: 691

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 85112

Personally i never refer to sagas as threads, but in defense of the people who do:

When applied to sagas, the term "thread" is just a mental model, not a description of what's actually happening under the hood. The term highlights that you can think of a saga as an independent piece of code which is executing side by side with other sagas. It won't be literally run in parallel (as you said, javascript is single threaded) but you can think of it that way.

Generator functions are a special type of function which can be "paused". They'll run for a bit and then return via a yield statement, but then they can be resumed at a later time. Since javascript is single threaded only one generator will be running at a given time, but redux-saga will manage starting and stopping them as needed, based primarily on the redux actions being dispatched.

Upvotes: 2

Related Questions