Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

509
Visualizações
takeEvery and takeLatest. Why? When to use? Use simultaneously?

I am not clear in when to use takeEvery and when to use takeLatest ? in redux-saga.

I got a basic difference by reading the official documentation. But what is the use of creating concurrent actions in takeEvery (for example, the user clicks on a Load User button 2 consecutive times at a rapid rate, the 2nd click will dispatch a USER_REQUESTED while the fetchUser fired on the first one hasn't yet terminated)

import { takeEvery } from `redux-saga/effects`

function* fetchUser(action) {
  ...
}

function* watchFetchUser() {
  yield takeEvery('USER_REQUESTED', fetchUser)
}

Can anyone please explain. As I am completely new to redux-saga.

Thanks in advance.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This is something you really need to think about per use case.

These are some cases when you might use takeEvery.

  1. For sagas that are not asynchronous and so there is no reason to cancel them. takeLatest would work here as well but it might give false indication when reading code that there is something to cancel.

  2. Sometimes when the action differs in some way each time. E.g. imagine you have a movie and you are adding tags with genre of the movie. Each time the action is triggered you get a different genre, even though it is the same action type. The user can add genres quicker than you get response from the server. But just because you added multiple genres quickly doesn't mean you want to stop the saga adding the previous one.

  3. Cheap implementation of when you have the same load action for multiple different items. E.g. You have list of movies and each has "load detail" button. (Let's ignore the fact that you should probably hide or disable the button once the loading starts). The data are always the same for one movie but differs in between them. When you click on load detail for movie 1 you don't want to cancel loading of data for movie 2. Since it is a dynamic list of movies the action type is the same every time, the difference will be probably something like id of the movie in the action.

    In ideal implementation you should probably cancel the previous load saga for the same movie/id, but that will require more complicated code and so if you are doing only some simple app you might decide to ignore that and just allow to run the sagas for same movie multiple times. That is why I call it "cheap implementation".

over 4 years ago · Santiago Trujillo Relatório

0

To summarize in a few words,

  • takeEvery allows concurrent actions to be handled. For example, the user clicks on a Load User button 2 consecutive times at a rapid rate, the 2nd click will dispatch a USER_REQUESTED action while the fetchUser fired on the first one hasn't yet terminated.
    takeEvery doesn't handle out of order responses from tasks. There is no guarantee that the tasks will terminate in the same order they were started. To handle out of order responses, you may consider takeLatest.
  • takeLatest instead start a new fetchUser task on each dispatched USER_REQUESTED action. Since takeLatest cancels any pending task started previously, we ensure that if a user triggers multiple consecutive USER_REQUESTED actions rapidly, we'll only conclude with the latest action

Docu: https://redux-saga.js.org/docs/api/

over 4 years ago · Santiago Trujillo Relatório

0

  • takeEvery - enables the use of several fetchData objects at the same time. At a given moment, we can start a new fetchData task while there are still one or more previous fetchData tasks which have not yet terminated.

  • takeLatest - Only one fetchData task can be active at any given moment. It will also be the work that was started most recently. If a new fetchData job is started while a previous task is still running, the previous work will be terminated immediately.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda